Linux Administrator

Top 20 Crontab Examples To Schedule Tasks

In this article we will learn Top 20 Crontab Examples To Schedule Tasks. Crontab is very important and useful to schedule jobs and task in Linux. As per our requirement we can schedule task to execute in background. We can also schedule command and scripts to execute on particular time interval. In this tutorial I am going to explain top 20 crontab examples.

You can install crontab command if you don’t have installed on your system.

CRONTAB FORMAT

crontab-format

Edit/Add Crontab

Follow the below command to update or add job in crontab.

# crontab -e

Above command will edit crontab of current logged in user. Follow the below command to edit/add other user crontab.

# crontab -u user's_name -e

List Crontab Jobs

Use the below command to list current user crontab entries.

# crontab -l

Follow the below command to view other user crontab entries.

# crontab -u user's_name -l

Top 20 Crontab Examples

1. Set a cron to execute/run at 1AM Daily.

In below example database backup scheduled on daily basis at 1am.

0 1 * * * /bin/sh /root/scripts/mysql_backup.sh

2. Set a cron to execute/run on every minutes.

This example will help to run a scripts on every minutes.

* * * * * /root/scripts/script.sh

3. Set a cron to execute/run twice a day.

In below example a script will execute at 11AM and 8PM daily.

0 11,20 * * * /root/scripts/script.sh

4. Set a cron to execute/run on every 15 minutes.

Follow the below example to execute/run a script on every 15 minutes interval.

*/15 * * * * /root/scripts/script.sh

5. Set a cron to execute/run on every Monday at 3PM.

In this example a script has been scheduled to execute/run on every Monday at 3PM.

0 15 * * mon /root/scripts/script.sh

6. Set a cron to execute/run on specific days.

You you set a cron to execute on specific days. In this example script will execute and run on each Monday and Saturday at 3PM

0 15 * * mon,sat /root/scripts/script.sh

7. Set a cron to execute/run on specific months.

If you want to scheduled a task to execute on selected months. Below example will help to set cron to execute on each March,Jun and October.

* * * mar,jun,oct /root/scripts/script.sh

8. Set a cron to execute/run a script on every five hours.

In below example a script will execute on every five hours.

0 */5 * * * /root/scripts/script.sh

9. Set a cron to execute/run a script on first Sunday of every Month.

It is not possible to execute a script only on first Sunday. That’s why we can use the condition in command field.

0 1 * * sun [ $(date +%d) -le 07 ] && /root/script/script.sh

10. Set a cron to execute/run a script on very 20 Seconds.

To execute the a script on every 20 second is not possible but using below example to you can set it.

* * * * * /root/scripts/script.sh
* * * * * sleep 20; /root/scripts/script.sh

11. Set a cron to execute/run a script twice on very Friday and Saturday.

Follow below example to execute a script twice on Friday and Saturday.

0 3,15 * * fri,sat /root/scripts/script.sh

12. Set a cron to execute/run a script yearly.

In below example a script has been scheduled to execute yearly.

@yearly /root/script/script.sh

13. Set a cron to execute/run multiple scripts in single cron.

Follow the below example to execute multiple scripts in single cron.

* * * * * /root/scripts/script_first.sh; /root/scripts/scrit_second.sh

14. Set a cron to execute/run a script on Weekly.

In this example a script has been scheduled weekly.

@weekly /root/script/scripts.sh

15. Set a cron to execute/run a script on Monthly.

You can scheduled monthly cron to execute a script.

@monthly /root/scripts/script.sh

16. Set a cron to execute/run a script on Hourly.

Below example is useful to set a cron to execute on hourly basis.

@hourly /root/scripts/script.sh

17. Set a cron to execute/run a script on Daily.

Follow the below example to set cron to execute on daily basis.

@daily /root/scripts/script.sh

18. Set a Email Address to send cron result.

By default cron result sends to current user where cron is scheduled. But you redirect it to another account.

# crontab -l
MAIL=Email_ID
0 2 * * * /root/script/mysql_backup.sh

19. Set a cron to execute/run a script on system reboot.

You can set some useful scripts to execute on system reboot. Below example will help to set cron to execute on system reboot.

@reboot /scripts/script.sh

20. Backup and Restore all crons.

Keep backup of all scheduled cron in a file. Follow the below to backup and restore all scheduled cron.

Cron Backup in a text file:

#crontab -l > backup_cron.txt

# cat backup_cron.txt
MAIL=Email_ID
0 1 * * * /root/scripts/mysql_backup.sh

Cron restore from backup file:

#crontab backup_cron.txt
# crontab -l
MAIL=Email_ID
0 1 * * * /root/script/mysql_backup.sh

Remove scheduled cron:

# crontab -r
# crontab -l
no crontab for root

I hope this article “Top 20 Crontab Examples To Schedule Tasks” will be helpful to understand to scheduled task in Linux or Unix System.

Thanks:)

Thank you! for visiting LookLinux.

If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at LookLinux Submit Article. Stay connected to Facebook.

About the author

mm

Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

Leave a Comment