Introduction to Linux Cron Jobs : Crontab Basics

Cron allows users to run commands or scripts at a given time or date. Cron is basically used to schedule scripts to be executed periodically. The cron jobs are used to automate process like backup, schedule updates and synchronization of files and many more.

Cron Daemon

Cron is a daemon to run schedule tasks. The cron daemon run background and constantly checks the /etc/crontab file, and /etc/cron.*/ directories. It also checks the /var/spool/cron directory. Cron wakes up every minute and checks schedule tasks in crontable.

Crontab

Crontab (CRON TABle) is a table where we can schedule repeated tasks. In Linux system each user can have their own crontab to create, modify and delete tasks. By default cron is enabled for users. Crontab file is located at /etc/crontab. Crontab file consists of command per line and have six field

At above the asterisk * operator specifies all the possible values for a field.

  • The First * sign defines which minute the job will run (values ranges from 0-59)
  • Second * defines hour of day (values ranges from 0-23)
  • Third * defines which Day of Month (values ranges from 1-31)
  • Fourth * defines which Month of year (values ranges from 1-12 or you can also use jan,feb,mar,apr…)
  • Fifth * defines which day of week (values ranges from 0-7 where 0 = Sunday or you can also use sun,mon,tue,wed,thu,fri,sat).

And at the end command name of path of the script will be provided with appropriate arguments.

Crontab examples :

Now lets see some example of crontab entries

  • Running a script at 10:30 am every day in particular time
30 10 * * * /home/user/myscript.sh
  • Running a script at 10:30 pm every day in particular time
30 22 * * * /home/user/myscript.sh
  • Running a script on sunday 12 pm
00 12 * * sun /home/user/myscript.sh

Operators used in crontabs

  • Comma , : Specifies a list of values, for example : 1,5,10,13,25 or sun,tue,fri
  • Dash - : Specifies range of values, for example 1-5, which is equivalent to 1,2,3,4,5.
  • slash /: Specifies a step value, for example `0-23/` in hour field means script executed at every hour.

Now lets see some example with operators

  • Running a script on 10 pm every week day
00 22 * * mon-fri /home/user/myscript.sh
  • Running a script on particular day of month at 12 am
00 00 1,7,12,28,25,27 * * /home/user/myscript.sh
  • Run a script every 5 minutes
*/10 * * * * /home/user/myscript.sh
  • Run a script at every two hours on sunday
* 0-23/2 * * sun /home/user/myscript.sh
  • Run a script at every 10 minutes
*/10 * * * * /home/user/myscript.sh

Some Special Keywords

  • @yearly : Run once a year, equivalent to “0 0 1 1 *”.
  • @annually : Same as @yearly.
  • @monthly : Run once at a month, equivalent to “0 0 1 * *”.
  • @weekly : Run once a week, equivalent to “0 0 * * 0”.
  • @daily : Run once a day, equivalent to “0 0 * * *”.
  • @midnight : (same as @daily).
  • @hourly : Run once an hour, “0 * * * *”.

For example :

  • Run update command with root user every month
@monthly root apt update -y
  • Run upgrade command every year
@yearly root apt upgrade -y

Crontab Commands

  • -e : Install or update job in crontab :
crontab -e

when you first run the command it will asks for an Editor to choose

You can choose any one, now add a cronjob as save the file.

  • -l : List crontab entires.
crontab -l
  • -r : Remove the cronjob entries
crontab -r

with -i message it will confirm the removal of job

$ crontab -i -r

crontab: really delete devm's crontab? (y/n) y
  • -u : Edit or see antother user cronjobs. Lists another user cronjobs
crontab -u UserName -l

Edit another user cronjobs

crontab -u UserName -e

System Wide Cron Schedule

You can also drop your scripts on predefined cron directories which can be automatically run by cron. As a root user or superuser you can use following directories to configure cron jobs. The directories are :

  • /etc/cron.daily : Run all scripts once a day.
  • /etc/cron.hourly : Run all scripts once an hour.
  • /etc/cron.monthly : Run all scripts once a month.
  • /etc/cron.weekly : Run all scripts once a week.

You can also put your script at /etc/cron.d/ and call them from /etc/crontab without providing full script path.

Checking Cron Job Logs :

  • On Ubuntu/Debian you can use
sudo systemctl status cron

or

sudo journalctl -u cron

You can get particular part of logs using grep patterns.

  • On FreeBSD logs cron are found at /var/log/cron
  • On CentOS cron logs are found at /var/log/cron

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.