This post describes steps to configure cron jobs in Ubuntu Linux to run every 5, 10 15 minutes, day, day or week, day or month, month or year, etc.
A cron job is a task that is executed at specified intervals typically on a Unix/Linux system. It is commonly used for operational and mundane tasks such as cleaning up logs, backing update system files and others.
Cron tasks or jobs can be scheduled to run at minute, hour, day, day or week, month, day of month or any combination of these.
Below is how to use cron to schedule tasks in Ubuntu Linux.
How to schedule cron tasks in Ubuntu Linux
As mentioned above, cron jobs in Linux are tasks that are commonly used for operational and mundane functions such as cleaning up logs, backing update system files and others.
Below is how one can use cron jobs to perform tasks at given intervals.
Crontab (cron table) is a text file which contains the schedule of cron entries to be run and at specified times. Each line in the user crontab file contains six fields separated by a space followed by the system command to run.
An example of a cron file will look similar to the one below:
* * * * * system command(s) to run | | | | | | | | | | schedules | | | | | ------- | | | | ----- Day of week (0 - 7) (Sunday=0 or 7) | | | ------- Month (1 - 12) (January - December) | | --------- Day of month (1 - 31) (1st day to 31st) | ----------- Hour (0 - 23) ------------- Minute (0 - 59)
The asterisk ( * )or star operator tells cron to run or execute all the time. For example, if an asterisk is in the Minute field, the task will run every minute.
Same for hour, day, day of week, month, day of month and so forth.
An ( – ) hyphen operator allows you to specify a range of values. For example, you can specify days of week as being ( 1 – 5 ) which represents (Monday to Friday) when it defined in the Day of week field.
A ( , ) comma operator allows you to define a list of repeated values. For example, if you defined (1, 2, 3) in the Hour field, it represents (1am, 2am, 3am). The task will run at 1am, 2am and 3am every Day if no day is defined.
It can also be represented as (1, 2, 5-8) which represents (1am, 2am and 5am to 8am).
The syntax of system-wide crontab file contains an additional mandatory user field that specifies which user will run the cron job.
* * * * * <username> command(s) to run
There are still more to cron and scheduling jobs, but the above should get you started.
How to schedule a cron job to run every 5 minutes
If you want to cron job to run every 5 minutes, enter the line below in the crontab file.
0,5,10,15,20,25,30,35,40,45,50,55 * * * * ~/script.sh
How to schedule a cron job to run every 10 minutes
To run a job every 10 minutes, enter the line below in the crontab file:
*/10 * * * * ~/scripts.sh
How to run a cron job every 10 minutes
To schedule a cron job to run every 15 minutes, enter the line below in the crontab file:
*/15 * * * * ~/script.sh
How to run a cron job every minute
To run a cron job every minute, enter the line below in the crontab file.
* * * * * ~/script.sh
An example cron entry for Let’s Encrypt certbot to automatically renew certificates looks like the one below:
# the cronjob.timer function takes precedence over this cronjob. For # more details, see the systemd.timer manpage, or use systemctl show # certbot.timer. SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(43200))' && certbot -q renew
That should do it!
This post showed you how to run cron tasks or jobs in Ubuntu Linux. If you find any error above or have something to share, please use the comment form below.