shaikhu-blog-v1626895672/image_3_mruy7g.jpg

How to schedule and manage tasks using crontab

Published on 2020-10-15, by Javed Shaikh

Subscribe for new article
*No spam. unsubscribe at anytime

Before going into details about crontab, lets suppose you are running an online store and you want to send an email about latest deals on every Friday at 9AM MST. So how you can achieve this. Of course you can write your own job scheduler of n number of lines of codes if you have that much of time or you can simply use crontab available in all Unix and Linux operating system to schedule a task.

What is Crontab

Crontab is short form of cron table. The cron is a utility available on all Linux and Unix operating system that runs a task or process at a given date and time. So crontab is actually a table that contains script or commands along with date and time to be run at.

How to view the crontab or cron table

On Ubuntu you can use crontab -l to view the current table

bash
1shaikh@shaikhu-com:~$ crontab -l 2# Edit this file to introduce tasks to be run by cron. 3# 4# Each task to run has to be defined through a single line 5# indicating with different fields when the task will be run 6# and what command to run for the task 7# 8# To define the time you can provide concrete values for 9# minute (m), hour (h), day of month (dom), month (mon), 10# and day of week (dow) or use '*' in these fields (for 'any'). 11# 12# Notice that tasks will be started based on the cron's system 13# daemon's notion of time and timezones. 14# 15# Output of the crontab jobs (including errors) is sent through 16# email to the user the crontab file belongs to (unless redirected). 17# 18# For example, you can run a backup of all your user accounts 19# at 5 a.m every week with: 20# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ 21# 22# For more information see the manual pages of crontab(5) and cron(8) 23# 24# m h dom mon dow command 25

Use crontab -e to edit and manage cron table

How to set date and time of job run

Below is the format of crontab that is supported by all Linux systems.

bash
1# * * * * * command to be executed 2# | | | | | 3# | | | | | 4# | | | | | 5# | | | | |_______________Day of the week (0 - 6)(Sunday to Saturday) 6# | | | |_______________Month (1 - 12) 7# | | |_______________Day of the Month(1 - 31) 8# | |_______________Hour(0 - 23) 9# |_______________Minute(0 - 59) 10

Lets get into some examples for better understanding

Example 1: Lets get into the first example we discussed at the beginning i.e. Run a job to send an email to all subscriber at 9AM every Friday

bash
10 9 * * 5 /usr/bin/python3 sendEmail.py

Example 2: In below example, we are running a job every 15 minutes( Note / operator)

bash
1*/15 * * * * doSomething.sh

Example 3: Here we are running a job every 1st and 5th hour ( Note the comma)

bash
1* 1,5 * * * doSomething.sh

Example 4: Below job runs on 1st January every year at 7AM

bash
10 7 * 1 * happyNewYear.sh

What if you want to run a job at random minutes every hour

As of now we have seen how crontab can be used to schedule a job at a specific date and time. But what if we want to run a job at a random time or say random minute every hour. Well we can achieve this as well by writing a sleep command as shown below.

bash
10 * * * * sleep $(($RANDOM%60))m;sh test.sh

Lets understand what we did above. As per crontab rules above command set will be run at every hour. At the start of every hour, the cron will encounter the two commands and the first command is to sleep for random minutes. So this cron will delay for this random minutes and then it will execute the second command which is our job once it wakes up from sleep 😃

If you want to do some more stuff than just sleep, we can write a shell script for the same instead of sleep.

As shown below you can run a python script from shell script after delaying n no of minutes.

Crontab:

bash
10 * * * * sh test.sh

test.sh

bash
1#!/usr/bin/sh 2#test.sh 3 4TIME=$((RANDOM%60)) 5sleep "${TIME}m" 6#Do some stuff 7#Do some more stuff 8/usr/bin/python3 /mybots/newsbot.py

So basically you are running a python code from a shell script once after delaying RANDOM minutes and after doing some more code stuffs:)

Conclusion

Cronjob is a very helpful utility table that is being used to schedule a task. Rules are very simple and easy to remember. You just have to follow the correct format to run a task or script. Hope this help you schedule your script/jobs :).

About the Author

I am a Backend System Engineer at a credit card company, specializing in C/C++ and assembler on IBM's TPF OS. I have a passion for web development and enjoy working with Node.js and Python in my free time.

Connect with author

Related articles ...

How to manage background processes in Linux

If you are new to Linux operating system and you start a command or process using terminal/session, you must have noticed that you need to wait till a process or command get finished before starting another command.

2020-10-18

How to monitor CPU utilization in Linux

Monitoring CPU utilization is one of the very important tasks of a back-end system engineer. CPU utilization is nothing but the total works or tasks being processed by your central processing unit or CPU

2020-10-16