16 basic cron command in Linux with Examples

In Linux/Unix, we can run and schedule many tasks automatically using the cron command. You can execute both commands once or at a regular interval. 'Cron' is widely used for scheduling repetitive jobs at regular intervals (run using commands listed in a table called 'crontab') and the 'at' procedure is used to schedule a job once in a specific time period. Crontab maintains individual crontab files for all users. These automated jobs will be hugely beneficial to many administrators who work with Linux servers.

Generally, the 'crontab' file is used for backups, updating systems, synchronizing servers, etc. The best thing about the cron is that you don't need to understand it in detail, but rather, you only need to know how to use it as part of setting up cron jobs.

This guide will teach you how to use various options of crontab entries. All the below examples of crontab job scheduling have been 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. Field 1-5 explains the date and time and the 6th field can be used for any Linux command or script that can be executed.

Note: The time field uses a 24 hour format.

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

(To add the entry to the crontab we should use the '-e' option. The vi editor will open the crontable once the entry has been  added, then save and close the file using the ‘: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 every day (Monday to Sunday) at 1.30 AM. Once the job is executed, Cron has the behaviour to send a notification email to the particular user about the task status either successfully or failing. 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 entries we added to 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 currently logged in users.

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

(Let's say, I want to modify the crontab entry for another user named 'linuxteck')

# crontab -u linuxteck -e

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

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

4. How to list different user 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 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 backup/DR will get the update every single minute from the production server).

# crontab -e

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

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

6. How to schedule a cronjob twice a day?

(It means a single command/script will be executed 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 a comma separate value in the field of  hours. This script will be executed in the mornings at 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 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 this number then some systems show 0-6 and some show 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 commas to be separated if more than a month.

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

(This means that normally we add the crontab entries one after one for different tasks, one different time, where here we can add multiple tasks with one particular time to execute one after another.There are several advantages of using this entry in cron. For eg: If I have multiple commands/scripts to be executed on 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 the scripts 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 freezes.

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




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: The 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 the cron with a 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: Used with the above syntax, we can test with a 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 to move 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/scripts 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, Apache status, etc.)

@reboot /usr/scripts/bootup_service_status.sh

Note: The above crontab script will be executed after every reboot. Here I have added the commands in the script to check 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!



3 replies on “16 basic cron command in Linux with Examples”

Cool, very well explain and very great that you put many example !! Very very great. I will follow you for sure.
Thanks again and looking for your next post. And at same time, how do we do a :
dir *any*.txt ? like to find a file with part of word in the file name. thanks

Very useful article. Thanks. Can you do an article on how to set up Linux to run a script when you boot into the system? Again, thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *