Code Done Right!

Cron – task automation

According to Wikipedia “cron (…) is a time-based job scheduler in Unix-like computer operating systems.”

And it does exactly that – cron allows for execution of any task at any time. All Linux distributions come with cron preinstalled, as this is an absolutely basic tool for any system administrator.

If you run a command with any regularity you should use cron instead of manually executing it. A good example might be ClamAV antivirus scan script as cron can run both executables and shell scripts.

How does cron work?

Every user, including root, has a crontab file in which one can schedule a task. There are some system-specific crontabs as well, but I will focus on root and user crontabs as those are of more use right now.

Every crontab can contain any number of lines with scheduled tasks. Take a look at the example below taken from Wikipedia

# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │                                   7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * <command to execute>

It shows the basic syntax of a scheduled task in cron. You can specify minutes, hours, days, months and days of the week. This allows for automation of a tasks throughout the whole year.

Look at the following example

0 * * * * /path/to/script.sh

This will run script.sh at every hour. It translates to At minute zero.

You can substitute asterisk with a number to define when the task should be performed. An asterisk * means every.

* * * * * /path/to/script.sh

This will run script.sh every minute. Script will run every 60 seconds!

0 0 1 10 * /path/to/script.sh

This one is for when you want to wake up Billie Joe Armstrong. It translates to Minute zero of hour zero of the first of October.

If you have any trouble setting up a time frame for your crontab use this online tool to help you.

How to setup a cron job?

You have to modify the crontab by running

crontab -e

If this is your first run of this command, you will see the following output

no crontab for root - 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]:

Just type 1 to use nano editor and press ENTER.

You will be taken to your user’s crontab file. It contains some basic instructions. Move to the bottom of the file and add jobs there.

Each line should contain only one scheduled job and should not start with #

For example, if you want to run your script at 1pm every Thursday put the following

0 13 * * 4 /path/to/script.sh

root vs user crontab

As mentioned earlier, each user, including root, has their own crontab. System keeps track of all of them. You can schedule tasks in any crontab you like, however, keep in mind that jobs scheduled in root crontab will be executed with root privileges and jobs schedules in user crontab will be restricted to what that specific user can do

If the task you are scheduling requires the use of sudo, then you should use root crontab instead of using your regular user with sudo. For jobs that do not require sudo use your regular account.

If you want to schedule a job in root crontab, prefix the command with sudo

sudo crontab -e

Few things to keep in mind when using a crontab

  • Test the execution of a script before adding it to crontab, to make sure it actually works
  • Script will be executed without any user input, do not move the script after adding it to the crontab
  • Do not schedule multiple tasks for the same time, it will unnecessarily consume resources and slow down your server
  • Schedule tasks one after another, make sure you roughly know how much time it takes for scheduled tasks – e.g. full data backup may take hours
  • Keep the file organized and put tasks in order from the most frequent at the top of your list to the least frequent at the bottom, it is for your sake
  • Keep script files in a separate folder dedicated just for them, this will help you to organise tasks

Test script execution by scheduling the task 2 minutes in the future, then close the file. If all goes well, you will have the result shortly and afterwards you can change the frequency.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.