In Linux/Unix platforms we can run and schedule many task in the background automatically using ‘Cron and at’ command. Both of the commands can be run either once in a specific time or at any regular interval. ‘Cron’ is the most widely used utility (run based on the commands listed in the crontable called ‘crontab’) for scheduling a repetitive task on a regular interval, and ‘at’ command is used to schedule a job once in a specific time. This crontab maintains crontab files for every individual users. These automated jobs will be a big advantage for many administrators who work on the Linux Server side. Mainly the ‘crontab’ is used for a making regular backup, synchronise between servers, system updates, etc. The beauty of the cron is, you don’t need to care about how the cron works but you need to care about how to handle it to set up the cronjobs.

This guide will teach you how to use various options of crontab entries. All the below examples of crontab job scheduling are tested on RHEL/CENTOS 7.6.

First, let us see the basic syntax of crontab entries and the expression :

Minute hour Day-of-Month Month-of-Year Day-of-Week Command 0-59 0-23 1-31 1-12 0-6 command / script

Crontab has come with 6 fields in total. The fields 1-5 explains about date and time and the 6th field is used for any Linux command or script can be executed.

Note: The time field uses 24 hours format.

1. How to schedule a cronjob for a particular time daily?

(To add the entry in the crontab we should use the ‘-e’ option. It will open the crontable in the vi editor. Once the entry is added then save and close the file using ‘:wq!’)

# crontab -e 30 01 * * * /usr/scripts/rsync_svnvmback.sh >/dev/null 2>&1

Note: Here, I have a bash script named ‘rsync_svnbackup.sh’ to be executed on every day (Monday to Sunday) at 1.30 AM. Once the jobs are executed, cron has behaviour to send a notification email to the particular user about the task status either success or failure. If the notification is not required, then we can disable it by using >/dev/null 2>&1 command at the end of the script, it will nullify all the notifications.

2. How to list the crontab entry?

(Using the below command to list the entry we added in the crontable. Here, I used the root account)

# crontab -l 30 01 * * * /usr/scripts/rsync_svnvmback.sh >/dev/null 2>&1

Note: ‘-l’ is the option to list the crontab list of the current logged in user.

3. How to modify the cronjob for a different user?

(Let’s say, I want to modify the crontab entry for other user named ‘linuxteck’)

# crontab -u linuxteck -e 30 01 * * * /usr/scripts/rsync_svnvmback.sh

Note: The above command can be executed by only the top user’s like ‘root user and superuser’, also if the normal user with explicit privilege. Here ‘-u’ represents the username and ‘-e’ option for edit.

4. How to list different user’s crontab entries?

(The below command will display the list of other users (linuxteck) entries )

# crontab -u linuxteck -l 30 01 * * * /usr/scripts/rsync_svnvmback.sh

Note: Remember, only the root /superuser can execute the above command or the normal user with the same privilege.

5. How to schedule a cronjob for every single minute?

(This requirement is very rarely used in the production, but there are some use cases. Let’s take one eg: If you are using a rsync script/command to get the update from the production server to the backup/DR, so that the back/DR will get the update of every single minute from the production server).

# crontab -e * * * * * /usr/scripts/rsync_svnvmback.sh

Note: The above crontab entry (rsync_svnvmback.sh) will run on every single minute of every hour throughout the year.

6. How to schedule a cronjob twice in a day?

(It means a single command/script will execute two times in a day. In a real-time example, some companies do take the Database backup in the morning and in the evening. Let’s say morning 6.00 AM and evening 8.00 PM )

00 06,20 * * * /usr/scripts/mysqldump.sh >/dev/null 2>&1

Note: I have applied comma separated value in the field of hours. This script will execute morning 6.00 AM and evening at 8.00 PM daily. The ‘>/dev/null 2>&1’ command to disable the notification as mentioned in my 1st example.

7. How to schedule a cronjob every 10 minutes?

(The below entry can execute the command/script every 10 minutes consecutively)

*/10 * * * * /usr/scripts/rsync_svnvmback.sh

Note: You can test it based on your requirement for either 5 minutes or 10 minutes, etc.

8. How to schedule a cronjob for selective days?

(Using the below entry we can execute a cronjob on selective days for eg: You can run a backup script only on Fridays and Sunday’s at 11.00 PM)

0 23 * * fri,sun /usr/scripts/rsync_svnvmback.sh

Note: Here I have used the shortened name of days instead of numbers, so that it will be easily readable to the user and use comma to separate the days. If you are using the number then some systems show 0-6 and some shows 1-7. Here many of the beginners got confused when ‘Sunday’ which number has to be used either ‘0 or 7’. In writing both ‘0 and 7’ stand on Sunday only.

9. How to schedule a cronjob for selective months?

(Using the below entry we can execute a cronjob for selective months for eg: The script should be executed only on the month of Jan & July at 11.00 PM.

0 23 * jan,jul * /usr/scripts/rsync_svnvmback.sh

Note: Using the fourth field crontab syntax to apply the name of the months. Use comma to be separate if more than a month.

10. How to run multiple cronjob in one line consecutively?

(It means, normally we add the crontab entries one after one for different task one different timing, where here we can add multiple tasks with one particular time to execute by one after one.There are several advantages of using this entry in cron. For eg: If I have multiple commands/script to be executed in my server for backup Database, backup application files, then compress the DB and files, then push those tar files into the DR location or backup location, then delete those tar/archive files from server, then clean up the tmp files, etc, etc. In this case if we execute all script at the same time concurrently then it will drastically affect the server performance. Eg: Running out of server disk space, memory, CPU and bandwidth usage will be raised very high, sometimes even server became freeze.

In that case we can use the below entry in crontab to use those scripts to be executing one after one consecutively, Eg: If we have 5 sets of scripts to be run at 1 AM in the morning, so the 1st script will start running at 1 AM and once this finishes, the 2nd starts immediately and so on.

Normal Course :

00 01 * * * /usr/scripts/mysqldump.sh

00 02 * * * /usr/scripts/application_backup.sh

10 01 * * * /usr/scripts/tar_db_appfile.sh

30 01 * * * /usr/scripts/cp_tar_remote_server.sh

10 02 * * * /usr/scripts/tardelete.sh

30 02 * * * /usr/scripts/clean_tmp.sh

Multiple tasks in one line crontab:

00 01 * * * /usr/scripts/mysqldump.sh && /usr/scripts/application_backup.sh && /usr/scripts/tar_db_appfile.sh && /usr/scripts/cp_tar_remote_server.sh && /usr/scripts/tardelete.sh && /usr/scripts/clean_tmp.sh -OR- 00 01 * * * /usr/scripts/mysqldump.sh; /usr/scripts/application_backup.sh; /usr/scripts/tar_db_appfile.sh; /usr/scripts/cp_tar_remote_server.sh; /usr/scripts/tardelete.sh; /usr/scripts/clean_tmp.sh

Note: Difference between using Double amper-sand ‘&&’ and semi colon ‘;’ is ‘&&’ means jobs will be executing one after one and ‘;’ means that 2nd or 3rd job will run no matter whether the previous jobs were successful or not.

11. How to use a special string in cron?

(Special strings are nothing but a replacement of values in the five slabs in cron with the single keyword to execute the task. We can use ‘@’ following by the keyword. The syntax with meaning is here below : )

Keyword Equivalent Meaning

@yearly 0 0 1 1 * –> Execute once in a year

@monthly 0 0 1 * * –> Execute once in a month

@daily 0 0 * * * –> Execute once in a day

@hourly 0 * * * * –> Execute once in a hour

@reboot — –> Execute once at startup

Note: Using with the above syntax, we can test with few examples here below.

12. How to schedule a cronjob using @yearly special string?

(The @yearly short code is equivalent to ‘0 0 1 1 *’ )

@yearly /usr/scripts/yearly_archival.sh

Note: The above crontab entry will execute the script for moving all the previous year data into the archival server. It will be executed at 00:00 on the 1st month (January) of every year.

13. How to schedule a cronjob using @monthly special string?

(The @monthly short code is equivalent to ‘0 0 1 * *’ )

@monthly /usr/scripts/monthly-backup.sh

Note: The above crontab entry will execute the script for the monthly backup. It will be executed at 00:00 on the 1st of every month.

14. How to schedule a cronjob using @daily special string?

(The @daily short code is equivalent to ‘0 0 * * *’ )

@daily /usr/scripts/daily-temp.sh

Note: The above crontab entry will execute the script for the daily-temp.sh, it will clear all the temporary files in the temp folder. Will be executed at 00:00 on the 1st of every day.

15. How to schedule a cronjob using @hourly special string?

(The @hourly short code is equivalent to ‘0 * * * *’ )

@hourly /usr/scripts/hourly_rsync_svnvmback.sh

Note: The above crontab entry will execute the script for the hourly_rsync_svnvmback.sh, it will sync all the data from production to backup every hour.

16. How to execute a script/command after every reboot using @reboot special string?

(The @reboot short code can be used to execute a set of commands/script after every restart of the server. For eg: After the reboot, we can execute a command/script to check the status of services like DNS or Apache status, etc.)

@reboot /usr/scripts/bootup_service_status.sh

Note: The above crontab script will execute after every reboot. Here I have added the commands in the script for checking the status of ‘httpd, named,dhcpd’ etc so that it will display the list of all the service status.

I hope this article will help you to learn a few steps towards crontab commands in Linux!! Drop me your feedback/comments.

Thank you!