Linux

Linux: how to schedule tasks with cron and crontab ?

I. Introduction

Under Linux, scheduled tasks are usually executed via so-called Cron tasks defined in the Crontab configuration file. This is another way of automating tasks on a Linux machine. It allows tasks to be scheduled to run at specific times or at regular intervals, simplifying automation under Linux.

In this article, we'll explore the use of Crontab, starting with a look at its syntax and then moving on to concrete examples of its use. The aim is for you to feel comfortable with task scheduling under Linux after reading this tutorial.

II. What is Crontab?

Cron is a Linux tool that executes scheduled tasks ("cron jobs") at specific times. It runs in the background on the Linux machine and will execute tasks defined in a file called crontab (short for "cron table"), specific to each user. So there's a crontab for superuser root, as well as for other system users (which we'll use as required).

With crontab, each user can schedule commands and scripts to run at defined dates and times. It's possible to run a task at a specific time or with a particular frequency: every 5 minutes, at 04:00 a.m. every day, or every other day. The crontab syntax, which we'll look at later, makes the system extremely flexible.

The system administrator can define global scheduled tasks in /etc/crontab or /etc/cron.d/. The /etc/crontab is a system file automatically generated by Linux. It will also read files in the following directories: /etc/cron.hourly /etc/cron.daily , /etc/cron.weekly and /etc/cron.monthly.

There are several commands you need to know to handle cron jobs on a Linux machine:

# edit the current user's crontab
crontab -e

# display the current crontab
crontab -l

# delete the user's crontab
crontab -r

# list the crontab of user "flo"
crontab -u flo -l

If you come across a machine where cron is not available, it is possible to install and activate it:

sudo apt update && sudo apt install cron
sudo systemctl enable cron

III. Crontab syntax

Before you consider editing a user's crontab, you should take the time tounderstand the syntax involved. In this file, each line corresponds to a different scheduled task. Then, each line of the crontab follows a well-defined syntax with several columns :

* * * * * <command to execute>
| | | | |
| | | | ----- Day of the week (0 - 7) [where 0 and 7 = Sunday]
| | | ------- Month (1 - 12)
| | --------- Day of the month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

Please note that the asterisk"*" means"all possible occurrences". So, if you put a"*" in the third column, the command will be executed every day of the month.

Generally speaking, there are several special characters you can use to refine schedules:

  • * every possible value.
  • , several possible values (for example : 1,15 means 1st and 15th).
  • - interval (for example : 1-5 means 1st to 5th).
  • / increment (for example : */10 every 10 units, i.e. every 10 minutes, if entered in the minutes column).

There are also special entries that are like aliases to make it easier to write certain frequencies:

  • @reboot at startup.
  • @hourly every hour.
  • @daily every day.
  • @weekly every week.
  • @monthly every month.
  • @yearly every year.

IV. Crontab usage examples

We're now going to take a look at a few examples of crontab usage to get you started. This will enable you to plan the task of your choice. This will also provide you with additional details on how crontab works.

A. Run a Bash script every day at midnight

To run a Bash script every day at midnight, e.g. a backup script, we can set up a scheduled task in the crontab. Start by editing your user's crontab:

crontab -e

The first time you go to edit the crontab, you'll be prompted to select the text editor with which to perform the editing. Nano is the simplest solution, which you can select by typing"1" then Enter.

You arrive at a file with a comment block. It explains crontab syntax in a nutshell. All you have to do is declare your scheduled task, like this to run it every day at midnight :

0 0 * * * /home/flo/scripts/backup.sh

Which gives:

Just save the crontab with"CTRL + O" if you're using Nano. Remember to make the Bash script executable, otherwise it won't run.

By default, the standard output (stdout) and error output (stderr) of commands executed via crontab are sent by e-mail to the user who owns the crontab. Thus, the above line, although functional, can be improved by playing with the redirection of the cron output.

  • Make the scheduled task silent by redirecting the output in /dev/null.
0 0 * * * /home/flo/scripts/backup.sh > /dev/null 2>&1
  • Redirect standard output to a log file.
0 0 * * * /home/flo/scripts/backup.sh >> backup.log
  • Redirect standard output and errors to a log file.
0 0 * * * /home/flo/scripts/backup.sh >> backup.log 2>&1

B. Purge a folder automatically

If you need to automatically clean (purge) a directory, this can be done via a crontab. The example below will purge the /home/flo/tmp every Monday at 06:00 a.m.

0 6 * * 1 /usr/bin/rm -rf /home/flo/tmp/*

When you run a binary with a cron job, it's a good idea to specify the full path, as shown here /usr/bin/rm. You can obtain this information easily with whereis :

whereis rm

C. Send a restart alert by e-mail

A script can be configured to notify an administrator when the Linux server reboots. In this case, we can use the @reboot alias to make it easier to write our cron line.

@reboot echo "The server restarted on $(date '+%Y-%m-%d at %H:%M:%S')" | mail -s "Server reboot alert IT-Connect" admin-demo@it-connect.fr

D. A variety of execution frequencies

When creating a scheduled task, the key lies in using the right syntax to schedule the task correctly and get the expected result. To help you, here are a few more examples.

# Runs every 5 minutes
*/5 * * * * <command>

# Runs every Sunday at 10:15 PM
15 22 * * 0 <command>

# Runs every weekday (Monday to Friday) at 08:00 AM
0 8 * * 1-5 <command>

# Runs on the first day of each month at midnight
0 0 1 * * <command>

# Runs every other day at 03:00 AM
0 3 */2 * * <command>

# Runs from the 1st to the 10th of each month at 03:00 AM
0 3 1-10 * * <command>

If you need help checking your syntax or simply generating it, there are two very useful sites you can use:

  • crontab-generator.org Simply select your requirements for execution frequency and the tool will generate the right crontab line.
  • crontab.guru You can specify your crontab line (without the command) and the tool will translate it into text. This is very useful for checking the syntax of your cron job.

V. Conclusion

Now you can understand what crontab is for, how to use it and how to log the tasks you set up. To schedule tasks under Linux, you can also use the timers from systemd. We'll look at this second method in a future article.

If you want to control and supervise the scheduled tasks of a set of servers, you may want to consider a solution like Apache Airflow.

author avatar
Florian Burnel Co-founder of IT-Connect
Systems and network engineer, co-founder of IT-Connect and Microsoft MVP "Cloud and Datacenter Management". I'd like to share my experience and discoveries through my articles. I'm a generalist with a particular interest in Microsoft solutions and scripting. Enjoy your reading.

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.