Automate your tasks using cronjobs

As a developer or system admin, there comes many time when we need to do certain tasks regularly and repeatedly. The tasks can be anything from running a backup, deleting or modifying files, sending logs, to running certain scripts. Doing the same task over and over again is kind of hectic. And, when we are unavailable, the task cannot be completed which can cause loss. This repeated tasks can easily done using Cron Job in Linux, where you just need to define the task and time to repeat the task.

Lets begin with the answer to what is cron? Cron is a time-based job scheduler in Unix like Operating System. Each tasks executed are called cronjob and the file containing the cronjobs are called crontab, which is defined in cronjob file. The crontab file is located at “/var/spool/cron/crontabs/<username>” of each user. The crontabs can be categorized into two: global crontabs, which are used by administrators and individual crontab which is used by every user.

Now, lets begin configuring the cronjobs. You can begin listing the cronjobs using: crontab -l. You might see no crontab for your user. So, let’s create one using: crontab -e, which edits the crontab file. The first time you execute this command, you will be asked to choose the text editor.

First time executing crontab -e

I have selected vim, and you can select any of the text editor of your choice. The text editor will appear and its time to add the cronjobs.

Default crontab file

The syntax for cron expression is :

min hour day month day_of_week <command to be executed>

Suppose we have to copy a text from one file to other every 1 am, the syntax will be:

0 1 * * * cat ~/file1 >> ~/logs

This means to copy the content of file1 to logs. This needs to be executed everyday of week, every month, every day (as * is wildcard for any) and at 1 am and 0 minute, i.e. 1:00 AM.

Similarly, if you need to execute the same thing every 5 hours, we need to use:

0 */5 * * * cat ~/file1 >> ~/logs

Similarly, we can also run a cron job every minute. The command can be anything the user has access to. The syntax might be confusing at first and there are different cronjob generators to help you generate and understand the syntax like Crontab Guru, Crontab Generator.

Apart from the syntax, there are predefined definitions to define time for the cronjobs. This includes @reboot, @yearly, @monthly, @daily and @hourly with each having certain equivalence.

Predefined definitions and its equivalence

Now lets get a list of cron jobs, using:

crontab -l

To assign cronjob for certain user, we can use:

crontab -u <username> -e

To remove all the cronjobs

crontab -r

Well, these are not the only stuffs you can do. We can create /etc/cron.allow and /etc/cron.deny file to allow or restrict the users from accessing the crontab file. If we have cron.allow file, we must specify the username to allow a user to use crontabs. And If we have cron.deny file, the users who needs the crontabs needs to be restricted. The syntax for both the file is simple, we just need to have username listed in new lines.

In absence of those files only the user having superuser privilege will get access to crontab. Superuser privilege means the user needs to be an ‘sudo’ group on Ubuntu, “wheel’ on ‘RFEL’ depending on the Linux distribution.

And finally on the last part, time zone on crontab. Suppose we are administrator of a multinational company having branch across various parts of the world. There might be many cronjobs that we need to execute for different branch of different places with different time zones. We can convert time of each time zone to main time zone used. But this increases chances of misconfigurations in time and also a lot of time can be used up. To solve this issue, we can add a line above cronjobs to specify the time zone for the jobs.


Cronjobs configuration for different time zones

Here, the above cronjob executes according to the timezone of Nepal and the cronjob below executes according to the timezone of New York, America

Other thing we can do is to define shell for the jobs. We need to add a line SHELL=<path_to_shell> above the cronjob to tell cronjobs which shell to use. All the cron jobs below the line will use the same shell so to change shell for cronjobs in between the file, we must again specify the shell.

Seems interesting right? The article will surely help anyone without prior experience on using cronjobs to understand and create one.

You may also like...