Schedule tasks in Ubuntu using Crontab

In this article, we would discuss how to schedule tasks in Ubuntu distribution using Crontab. Tasks can be scheduled in Ubuntu through three tools – at, batch and cron daemon. The at and batch tools can’t be utilized for tasks which are required to be executed frequently. But, with cron daemon we can schedule tasks to be executed on a hourly, daily, weekly, monthly basis or we can setup schedules as per our requirements. There are mainly four directories –

/etc/cron.daily
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly

So, if we place our scripts inside any of the four directories. Then, our script will be executed as per the defined schedule for the specific directory. For instance, we could list directory /etc/cron.daily/ through the command –

ls -l /etc/cron.daily/

It would list all the scripts which are executed, through run-parts tool, once in our distribution on a daily basis. We can place our scripts in any of the above mentioned directories.

Apart from that, there are mainly two types of tasks which can be scheduled and executed – System jobs and User jobs. System jobs can only be scheduled through /etc/crontab file whereas User jobs can be scheduled by the specific user. System jobs are executed through /etc/crontab file. And, user jobs can be executed through /var/spool/cron/crontabs/<user-name> file.

Schedule User jobs in Ubuntu using Crontab

First, we would discuss how to schedule and execute user jobs.

To list the current crontab, issue the following in terminal –

crontab -l

The crontab file contains the list of tasks which are to be executed at designated times. It is recommended that we provide the complete path of the command when we edit through tool crontab. To know the pathnames, we use which command-line tool. For instance, if we want to know the pathname of ls command then –

which ls

It would return with the output –

/usr/bin/ls

The format of entries for crontab file would be –

0-59    0-23    1-31    1-12    0-7    command to be executed

where,

0-59 is the minutes field,

0-23 is hours

1-31 is day

1-12 is month

0-7 is day of week

Now, understand the format with the help of an example. To schedule and execute ls command every 5th minute of the hour and send the output to a file crontab.log. Then, we append the crontab with –

5 * * * * /usr/bin/ls >/home/<user-name>/Desktop/crontab.log

We can also provide a range of values as illustrated above or we can separate them with comma(,).

For instance, to execute tasks at 4 AM, 5 AM and 6 AM we would write –

* 4-6 * * * /usr/bin/ls >/home/<user-name>/Desktop/crontab.log

or,

* 4,5,6 * * * /usr/bin/ls >/home/<user-name>/Desktop/crontab.log

Now, we will schedule tasks to be executed as discussed above. Issue the following in terminal –

crontab -e

If the above command gets executed for the first time, then it would ask us to choose the default text editor.

no crontab for <user-name> - using an empty one

Select an editor. To change later, run 'select-editor'.
1. /bin/nano <---- easiest
2. /usr/bin/vim.tiny
3. /bin/ed

Choose 1-3 [1]:

Choose one accordingly. Or, we can switch to a different one later through command –

select-editor

Afterwards, a crontab file would get opened. Append the file with following entries as discussed above –

5 * * * * /usr/bin/ls >/home/<user-name>/Desktop/crontab.log

replace <user-name> with appropriate value. When we save and quit from the file, it should return the output –

crontab: installing new crontab

To list the current crontab –

crontab -l

and, to confirm if necessary changes have been made –

sudo ls -l /var/spool/cron/crontabs/<user-name>
sudo cat /var/spool/cron/crontabs/<user-name>

Schedule System jobs in Ubuntu using Crontab

Systems jobs can be scheduled and executed only through /etc/crontab file. The format of the entries remains almost the same, with exception to provide <user-name>.

0-59    0-23    1-31    1-12    0-7    user-name    command to be executed

It is not recommended to make changes to /etc/crontab file unless we absolutely know what we’re up to.

Lastly, to remove a crontab for a user

crontab -r

We can confirm if its gone or not –

sudo ls -l /var/spool/cron/crontabs/<user-name>

it would return with the ouput –

ls: cannot access '/var/spool/cron/crontabs/<user-name>': No such file or directory

In conclusion, we have discussed how to schedule tasks in Ubuntu using Crontab tool.

Similar Posts