16 basic cron command in Linux with Examples

By using the cron command, we can schedule and run many tasks automatically in Linux/Unix. You can execute them once or on a regular basis. Cron is widely used to schedule repetitive tasks at regular intervals (using commands listed in a file called 'crontab'), and the 'at' function is a way of scheduling a task once during a specific period.For each user, Crontab maintains a crontab file. Automated jobs will be extremely helpful for many administrators who manage Linux servers.

In general, the crontab function is used for backups, updating systems, synchronizing servers, etc. The great thing about cron is that you don't have to understand it in detail, instead, you just need to know how to use it as part of setting up cron jobs.

In this guide, you'll learn how to use various crontab options. We have tested all of the examples of crontab job scheduling using RHEL/CentOS.

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?

Normally, we would add crontab entries one by one for one particular task, at a particular time, but in this case, we can add multiple tasks at the same time and they will all be executed at the same time. This entry into CRON offers several advantages. As an example: If I had several scripts/commands to be executed on my server, such as backup the Database, back up the application files, then compress the Database and files, then push the tar files to the DR location or backup location, then delete the tar files and archive files from the server, then clean up the temporary files, etc. If all these scripts run at the same time concurrently, server performance will be negatively affected. For example, there is a possibility that a server's disk space will run out, an immense amount of memory will be used, and CPU and bandwidth usage will be extremely high, resulting in server freezes.

In that case, we can use the following crontab entry to have those scripts run successively one after the other. For instance, if 5 sets of scripts need to run at 1 AM in the morning, so the first script will start running at 1 AM, once it has finished running, then the second will start 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: 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.

Note:

It is also possible for users without a system administrator role to schedule tasks online using the Cron GUI tool. This tool allows you to create crontab scripts easily.

Easy Cron

Cron Job Generator

Crontab Generator

Conclusion:

Hopefully, this article will help you learn a few steps towards using Linux's crontab commands . Thank you for taking the time to read! Drop me your feedback/comments. Feel free to share this article with others if you like it.

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.

L