This article will explain scheduling tasks and running scripts at regular intervals using cron jobs.
What is Cron?
Cron is a task scheduling program that runs in the background as a process. It can be used to periodically run certain jobs that are scheduled using its predefined set of rules. Cron comes preinstalled on most Linux distributions or available in the repositories to be installed manually.
Cron Presets
Cron on Ubuntu comes with a set of pre-defined schedulers. You can check them by running the command below:
Usually these folders are “/etc/cron.hourly”, “/etc/cron.daily”, “/etc/cron.monthly” and “/etc/cron.weekly”.
Just put your desired scripts in one of these folders to run automated tasks hourly, daily, monthly or weekly.
Note that cron uses “/bin/sh” shell by default. If you want to use “/bin/bash” as the shell in cron scripts, you have to use “!# /bin/bash” hashbang at the top of the script file or define shell as “SHELL=/bin/bash” variable in the first line in a scheduled bash script. Overriding “SHELL” environment variable also works for crontab editor explained below.
Not all system environment variables may be exposed to the cron jobs. So it is generally a good idea to manually define important environment variables in a script scheduled by cron.
Crontab
Crontab allows you to define your own scheduled tasks. You can use it to configure cron schedules different from the four standard cron presets explained above.
To set a scheduled task in crontab, first launch the crontab editor using the command below:
Now at the end of the file, you can add an entry in the following format:
Where:
- minute: any value in the range 0 to 59
- hour: any value in the range 0 to 23, 0 being midnight
- day: any value in the range 1 to 31
- month: any value in the range 1 to 12
- weekday: any value in the range 0 to 6, 0 being Sunday
The crontab entry below will take a screenshot of your desktop and store it in your home directory once at 15:13 hours on 2nd of March plus every Tuesday in March at 15:13 hours.
To list all crontab entries, use the command below:
More Examples
Cron tasks can be best explained through examples, so below are some examples that will periodically take screenshots of your desktop and store them in home folder.
Besides the five scheduling variables, cron also uses some special string to schedule jobs. These strings are:
- @hourly: job is run every hour
- @daily / @midnight: job is run every day
- @weekly: job runs once a week
- @monthly: job runs once a month
- @yearly / @anually: job runs once a year
- @reboot: job runs once every reboot / login
The crontab entry below will take screenshot once on every reboot:
You can replace “@reboot” with any other special strings mentioned above.
Using an asterisk or wildcard in place of any of the five variables will repeat job on all new instances of that variable. The entry below will take a screenshot every minute:
Every hour:
Everyday at 18:00 hours:
Every month on 6th at 18:00 hours:
Use “/” to run a script at a fixed periodic interval of 30 minutes (runs every 30 minutes):
You can use comma separated variables to define repeat execution of script at regular intervals. The example below will take a screenshot of your desktop on every 1st, 4th, and 5th minute of a new hour.
Verifying Crontab Entries
Defining crontab entries can be tricky but debugging them or verifying if they work at correct timestamps is even trickier. One option is to use a virtual machine and log timestamps of a script running periodically on a cron job by including something like “date” command and redirecting the output to a text file. However, a better solution is to use one of the numerous online cron jobs testing services available on the web. I recommend giving cron tester website available here a try.
Conclusion
Running cron jobs is an excellent way to automate repeated tasks, especially on servers. The tool is extremely powerful and customizable. However, it is important to ascertain that your scheduler definition works correctly, especially if there are mission critical tasks.