Crontab Examples


20 Useful Crontab Examples

Let us start with very basic crontab example.

1. Schedule a crontab at particular time (at 1 am)


This cron will be useful for you to do some cleanup activities on servers when there is a less usage.

0 1 * * * /path/to/script.sh


This job runs every day 1 AM.

2. Schedule a cron to run twice a day


Do you have a script that is required to be run twice a day? Use the below cron job example.

0 10,22 * * * /path/to/script.sh


Above cron runs twice a day at 10 AM and 10 PM.

3. Schedule a cron to run every Sunday 1 AM

You may need to schedule a cron to do weekend activities like taking full backup or configuration backup.

0 1 * * sun /path/to/script.sh


OR

0 1 * * 0 /path/to/script.sh


OR

0 1 * * 7 /path/to/script.sh
0 or 7 means Sunday.


4. Schedule a crontab every minute


This one may be funny; sometimes you may require run cron for every minute.

* * * * * /path/to/script.sh

5. Schedule a crontab every 5 minutes


Sometimes you may need to run a program like pinging servers for their availability.

*/5 * * * * /path/to/script.sh

6. Schedule a crontab every hour (hourly cron)


Below schedule runs every hour and is used for the hourly task.

0 * * * * /path/to/script.sh


OR
@hourly /path/to/script.sh
7. Schedule a crontab every 2 hours
You can use the below cron job example that set to run a script every two hours.

0 */2 * * * /path/to/script.sh
8. Schedule a crontab daily (daily cron)
Below cron job example will be suitable if you want the script to be executed on a daily basis, exactly at @ 12 AM.

0 0 * * * /path/to/script.sh
OR

@daily /path/to/script.sh
9. Schedule a crontab every alternate day
Use the below cron example to run a job at every alternate day.

0 0 */2 * * /path/to/script.sh
10. Schedule a crontab on select days
To schedule a cron job on select days, i.e., to run cron on Tuesday and Thursday at 1 PM.

0 13 * * tue,thu /path/to/script.sh
11. Schedule a crontab every week (weekly cron)
You can quickly schedule a weekly cron job using below example.

@weekly /path/to/script.sh
This cron runs every week Sunday at 12 AM.

12. Schedule a cron on the 15th day of every month

You can use the below settings if you want a cron job to be executed on 15th of every month.

0 11 15 * * /path/to/script.sh
13. Schedule a cron every month (monthly cron)
You may want to create a cron job that runs on the first day of the month. This cron runs on 1st of every month at 12 AM.

@monthly /path/to/script.sh
14. Schedule a cron on select month
Below cron example runs every day at 12 AM in January, April, and June.

0 0 * jan,apr,jun * /path/to/script.sh
15. Schedule crontab after every reboot
Want to run script or command after every reboot then below job could be useful for you.

@reboot /path/to/script.sh
16. Send email in crontab
You can use the below cron settings for sending results of the scheduled task.

MAILTO="raj"
1 1 * * * /path/to/script.sh
17. Change shell in cron
Want to execute the cron on different shell rather than the default, /bin/bash.

SHELL=/bin/sh
1 1 * * * /path/to/script.sh
18. Environmental variables in cron
Sometimes you may need to use environmental variables for successful execution of script then below setting could be useful for you.

PATH=/sbin:/bin:/usr/sbin:/usr/bin
1 1 * * * /path/to/script.sh
19. Set Home for cron
Do you want to set the home directory to use when executing scripts? Then use this.


HOME=/
1 1 * * * /path/to/script.sh
20. Cron jobs Every Second
Cron cannot be used to schedule a job in seconds interval.

Popular posts from this blog

script and validation for the lvm

Important concepts and bits

shell script to create and extending the volume