Table of content

Cron jobs in Linux

There are four different types of cronjobs. They are hourly, daily, weekly, monthly. All these jobs are set up in /etc/cron._(directory). The timing for each of the jobs except hourly is set up inside /etc/anacrontab directory.

Linux system pack has a useful task scheduler named crontab. Crontab is popular because it can be scheduled to run an automated process as root. Therefore, having an automated process running as root makes system changes easier. You just need to change the task and then wait until the task is re-initiated.

Crontab of Linux has six fields. The first five fields define the time and date of execution, and the 6'th field is used for command execution.

Crontab fields

The beginning five fields represent time to run tasks and the last field is for command.

  1. Minute (hold values between 0-59)
  2. Hour (hold values between 0-23)
  3. Day of Month (hold values between 1-31)
  4. The month of the year (hold values between 1-12 or Jan-Dec, you can use the first three letters of each month’s name i.e Jan or Jun.)
  5. Day of week (hold values between 0-6 or Sun-Sat, Here also you can use the first three letters of each day’s name i.e Sun or Wed. )
  6. Command

User can edit their crontab jobs with the help of the following crontab command:

crontab -u -e

The above command will open the personal crontab configuration of your computer system, which can be edited by using your default text editor.

There is no need to restart your crontab as it will pick up your changes automatically when you use the following command.

crontab -l

To remove your crontab tasks, use the following command.

crontab -r

To add or update the job in crontab, use the below-given command.

crontab -e

Command to edit other user's crontab

crontab -u username -e

How to List Crontab

Command to view crontab entries of the current user

crontab -l

Command to view crontab entries of a specific user:

crontab -u username -l

The hourly job is set up inside the /etc/cron.d/hourly directory.

The command ls -l | grep cron shall give us all the jobs available residing within the /etc folder.

Now to get the scripts within the daily job, we shall first move to the location with the cd cron.daily command and then run the command ls - l.

We can move a job from one folder to another for example from daily to weekly job the mv command is executed.

crontab with -i option will prompt you confirmation from the user before deleting the user’s crontab.

crontab -i -r 
crontab: really delete root's crontab?
Allowed special character (*, -, /, ?, #)
  1. Asterisk(*) – Match all values in the field or any possible value.
  2. Hyphen(-) – To define range.
  3. Slash (/) – 1st field /10 meaning every ten minutes or increment of range.
  4. The comma (,) – To separate items.
System-Wide Cron Schedule

System administrator can use predefine cron directory as shown below.

  1. /etc/cron.d
  2. /etc/cron.daily
  3. /etc/cron.hourly
  4. /etc/cron.monthly
  5. /etc/cron.weekly
Schedule a Jobs for Specific Time

The below jobs delete empty files and directory from /tmp at 12:30 am daily. You need to mention the user name to perform the crontab command. In the below example root user is performing cron job.

crontab -e 30 0 * * * root find /tmp -type f -empty -delete
Strings Meanings
@reboot Command will run when the system reboot.
@daily Once per day or may use @midnight.
@weekly Once per week.
@yearly Once per year. we can use the @annually keyword also.

In the below example, command1 and command2 run daily by giving the "&&" symbol.

# crontab -e

@daily <command1> && <command2>

The following line sets the hardware clock on the computer using the system clock as the source of an accurate time. This line is set to run at 5:03 a.m. (03 05) every day.

03 05 * * * /sbin/hwclock --systohc

to perform a DNF or yum update at 04:25 a.m. on the first day of each month

25 04 1 * * /usr/bin/dnf -y update

If you want to run a particular job every Thursday at 3 p.m.:

00 15 * * Thu /usr/local/bin/

maybe you need to run quarterly reports after the end of each quarter. The cron service has no option for "The last day of the month," so instead you can use the first day of the following month, as shown below. (This assumes that the data needed for the reports will be ready when the job is set to run.)

02 03 1 1,4,7,10 * /usr/local/bin/

The following shows a job that runs one minute past every hour between 9:01 a.m. and 5:01 p.m.

01 09-17 * * * /usr/local/bin/

where I need to run a job every two, three, or four hours. That can be accomplished by dividing the hours by the desired interval, such as */3 for every three hours, or 6-18/3 to run every three hours between 6 a.m. and 6 p.m. Other intervals can be divided similarly; for example, the expression */15 in the minute's position means "run the job every 15 minutes."

*/5 08-18/2 * * * /usr/local/bin/

Run the shell script /home/tharani/ on January 2 at 6:15 A.M:

15 6 2 1 * /home/tharani/

Days and months can be listed by name (Monday) or abbreviation (Jan). Zeroes at the beginning of a number are valid, which helps you make multiple entries line up visually. For instance, the next example runs the same script as above, at 12:01 A.M., every Monday in January:

01 00 * Jan Monday /home/tharani/

Run /home/tharani/ every hour, on the hour, from 9 A.M. (09:00) through 6 P.M. (18:00), every day:

00 09-18 * * * /home/tharani/

Same as the above, but run it every twenty minutes:

*/20 09-18 * * * /home/tharani/

Run /home/Tharani/ every Monday, at 9 A.M. and 6 P.M:

0 9,18 * * Mon /home/Tharani/

Run /usr/local/bin/backup at 10:30 P.M., every weekday:

30 22 * * Mon,Tue,Wed,Thu,Fri /usr/local/bin/backup

Add the below example to execute the specified log backup shell script at 11:00 on every day. We can specify the comma-separated value in a field specifies that the script needs to be executed in all the mentioned time.

00 11 * * * /home/backups/scripts/
Scheduling a Job for a different user

The basic usage of cron is to execute a job at a specific time as shown below. This will execute the Full backup shell script ( Every day at 9 AM and 6 PM

00 09-18 * * * /home/test1/

00 – 00th Minute,09-18 – 09 AM & 06:00 PM,* --Every day of the Month,* -- Every Month,* --Every day of the week

To configure a cron job to run every night at 2:30

30 2 * * * /your/command

There are a couple of ways to run a crontab entry every five minutes.

0,5,10,15,20,25,30,35,40,45,50,55 * * * * /var/cherchertech/

The above command executes fine but corn provides us a way to reduce the size of the command as below.

*/5 * * * * /var/www/cherchertech/bin/

To get the timings and all other information on the daily job we have to move to /etc/cron.daily location, then run the command:

cat /etc/anacrontab
Reasons for using Cronjobs in Linux:
  • Helps OS to take a scheduled backup of log files or database.
  • Delete old log files
  • Archive and purge database tables
  • Send out any notification email such as Newsletters, Password expiration email
  • Regular clean-up of cached data
  • Crontab is an ideal option to automate Unix jobs.
  • It is used to automate system maintenance
Cron Vs Anacron

Cron and anacron has advantages as well as disadvantages one over the other.

Cron Anacron
Every minute execution of the job is possible. Jobs can be executed every day only
Cron job is user-independent anyone can schedule the job. The job can be scheduled only by the super-user.
If the system is powered off and a job is about to execute at the time, the job will not be executed. If a job is scheduled, and the system is power off during that time, it executes the job when the system is turned on.
Ideal for servers Ideal for desktops and laptops
Use cron when a job has to be executed at a particular hour and minute Use anacron when a job has to be executed irrespective of an hour and minute
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions