How to use Cron Jobs

How to use Cron Jobs on Linux

Cron is a widely used tool which is used to schedule tasks or so-called jobs on any Unix-like operating system. It can be set up to run the corn jobs daily, weekly, or monthly, but it can also be set up to run any given period or once only.

For example, you want to run a backup script during off-work hours. Using cron jobs, you can easily schedule the script to run at a specific time daily, every second day, weekly, or any other period.

Cron jobs can be scripts or bash commands that are set up to run at a specific time using the unix-cron string format ( * * * * * ). In this article, you will learn what each of the five fields represents.

Crontab Usage

The crontab command can be used to edit or list cronjobs for the current user or for other users.

If you want to edit or add a new cronjob for the current user, you can use the command:

crontab -e

Running the command the first time will prompt you to choose a default editor.

no crontab for john - using an empty one

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.tiny

Choose 1-2 [1]:

After choosing your editor, it will open an editor where you can add, edit, or remove cronjobs. Once the editor is opened, you can use the cron syntax to add your first cronjob.

The cron syntax uses expressions that map each minute, hour, day(month), month, day(week) to schedule the cronjobs.

 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
# |  |  |  |  |
# *  *  *  *  *  command to be executed

For example, you can want to run the backup script every day in 3:30 AM you can add the cron job

30 3 * * * /opt/backupscript.sh

To run a script every minute, you can use the default expression:

* * * * * /etc/somescript.sh

You can find some other scheduling examples below:

*/15 * * * * command/script - Every 15 minutes

0 8 * * 1-5 command/script - Monday to Friday at 08:00 AM.

0 16 * * 6,0 command/script - Every weekend at 04:00 PM.

0 0 15 * * command/script - Midnight every 15th of the month

All this cron syntax looks very confusing, but you can use some tips to set up your corn jobs easily.

Modern cron will also accept the following cron expressions next to the command or script you want to run. @hourly, @daily, @weekly, @monthly and @reboot The @reboot expression means it will run after rebooting.

You can also use the abbreviated form for the days of the week or month. Here are some examples:

5 4 * * Mon           -       04:05 AM every Monday
0 0 * Mar Fri         -       Every Friday in March at Midnight 

Cronjob for users

To edit or list cronjobs for other users, you can use the following command:

 crontab -u john -l

This will list all the cronjobs for the user john. The same is true for editing the cronjobs for the user john.

crontab -u john -e

This command will open the editor and each added cronjob will be for the user john. Another way you can easily edit the cronjobs for different users is using the configuration file /etc/crontab, here you can add the following line to run the cron job for the user john.

15 15 * * * john /path/somescript.sh 

This cronjob run will run /path/somescript.sh for the user john at 3:15 PM every day.

Daily, weekly, monthly cronjobs.

To run cronjobs on a daily, weekly, and monthly basis you will only need to create a script file in the directory that shows how often you want this cronjob to run.

For your backup script to run once every week, you can add the file in the /etc/cron.weekly directory.

You may wonder on which day this script will be run every week. To check or modify the time when the daily, weekly, or monthly cronjobs are run, you can check the file /etc/crontab. It should look something like this.

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# 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-name command to be executed
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || { cd / && run-parts --report /etc/cron.daily; }
47 6    * * 7   root    test -x /usr/sbin/anacron || { cd / && run-parts --report /etc/cron.weekly; }
52 6    1 * *   root    test -x /usr/sbin/anacron || { cd / && run-parts --report /etc/cron.monthly; }
#

Using the job definition in the comments, you can see that in our case the daily cronjobs are run at 6:25 every day for the daily script, 6:47 every Sunday for the weekly, and 6:52 every 1st of the month for the monthly cronjobs.

Conclusion

Although setting up or modifying cron jobs can sometimes seem scary or confusing, we hope this article helped you understand how to use cron jobs and made it easier for you. If it did, please let us know with a comment below. If you still find this too confusing, you can always give our Linux VPS admins a try and they’ll take care of all the heavy lifting for you. You can also share this post online if you know of others who may be interested in this article. Thank you.

Leave a Comment