How to use cron?

Cron is a practical tool essential for any system administrator, which regularly allows launching applications, practical for a server to launch backup scripts, etc. Existing for many years, it can sometimes be a bit complex to master. That's why in this guide, we will try to explain the basics of the tool, how to install it, and how to use it to schedule recurring tasks.

History

Cron is a time-based task scheduler in Unix-like operating systems (OS) and is one of the most useful utilities you'll find. Created in AT&T Bell Labs in 1975 and named after Chronos, a Greek personification of time, cron has not only come a long way since its initial creation but has managed to remain relevant to this day, which now makes it one of the most widely used task scheduling software.

How to install cron

Almost all Linux distributions include some form of cron installed by default. However, if you are using an Ubuntu machine that does not have cron installed, you can install it using APT.

sudo apt install cron

You will need to make sure it is configured to run in the background as well:

sudo systemctl enable cron

How to use cron

The cron software works with the help of configuration files called "crontab files", which define, according to a particular format which tasks should be executed in the background.

To see the contents of these crontab files, simply run:

crontab -l

To modify the contents of these files, issue the command:

crontab -e

Format of crontab files

Here is schematically the syntax to be followed of a crontab:

# Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user command to be executed

Each star can be replaced with either a number, indicating the hour or minute at which the command should run, a star, indicating that the job should run every unit (every hour, every minute, ...), or use a format such as */4, allowing to indicate a run every 4 hours, 4 minutes, or 4 days

For example, if you want to run a command on Mondays at 3:00 am, every 5 minutes, you would simply create the following definition:

*/5 3 * * 1 user command

Check my configuration

There are many solutions to check the correct definition of your job. The simplest is to check the cron service execution logs, to verify that everything is working correctly (note that you may want to wait a minute to make sure the cron service has read your job definition before analyzing the logs):

grep crontab /var/log/syslog | tail

You can also use online services, such as this tool

Conclusion

In this article, we hope we were able to explain the basics of the cron tool, which will allow you to run jobs recurrently on a Unix system. This should be enough for you to run simple and regular tasks, such as sending an email or running a script.

About Us

At Cronit, we edit a recurring job execution software solution, very similar to cron, but it works directly from your web browser and does not require any server setup. This allows you to set up recurring jobs very quickly without having to worry about installing cron or maintaining machines set up only to run your recurring jobs. If you want to know more, feel free to check out our website, and try our free application: https://cronit.app