Crontab allows Linux and Unix users to run commands or scripts at a given date and time. You can schedule scripts to be executed periodically. Cron is one of the most useful tool in a Linux or UNIX like operating systems. It is usually used for sysadmin jobs such as backups or cleaning /tmp/ directories and more.

It is an acronym for “Command Run On Notice” or “Commands Run Over Night”

Some Crontab Examples to Schedule Tasks

Image result for crontab

Edit/Add Crontab

#crontab -e
# crontab -u user_name -e

List Crontab Jobs

# crontab -l
# crontab -u user_name -l

Examples

1. Set a cron to execute/run at 2:23 PM 24th March for hostname command.

23 14 24 03 05 /bin/hostname

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

* * * * * /root/script.sh

3. Set a cron to execute/run on every 5 minutes.

*/5 * * * * /root/script.sh

4. Set a cron to execute/run on every Friday at 3PM.

0 15 * * 05 /root/script.sh

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

Set a cron to execute on specific days. Script will execute and run on each Monday and Friday at 6PM

0 18 * * mon,fri /root/script.sh

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

Set a cron to execute on each March,April and May.

* * * mar,apr,may /root/script.sh

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

Setc a cron to execute script on every five hours.

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

8. 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

9. Set a cron to execute/run a script on every 30 Seconds.

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

* * * * * /root/script.sh
* * * * * sleep 30; /root/script.sh

10. Set a cron to execute/run a script yearly,Monthly,Weekly,Daily and Hourly.

Script has been scheduled to execute Yearly,Monthly,Weekly,Daily and Hourly.

Table: Cron special keywords and its meaning
Keyword Equivalent
@yearly 0 0 1 1 *
@daily 0 0 * * *
@hourly 0 * * * *
@reboot Run at startup.
@yearly /root/script.sh
@monthly /root/script.sh
@weekly /root/scripts.sh 
@daily /root/script.sh
@hourly /root/script.sh

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

Set cron to execute on system reboot.

@reboot /root/script.sh

12. Schedule a Job for Specific Range of Time (e.g. Only on Weekdays)

checks the status of the database weekdays during the working hours 9 a.m – 6 p.m

00 09-18 * * 01-05 /root/check-db-status
  • 00 – 0th Minute (Top of the hour)
  • 09-18 – 9 am, 10 am,11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm
  • * – Every day
  • * – Every month
  • * – Every day of the week

13. Backup and Restore all crons.

Backup in a text file:

#crontab -l > backup.txt
# cat backup.txt
MAIL=mailid
0 1 * * * /root/mysql.sh

Cron restore from backup file:

#crontab backup.txt
# crontab -l
MAIL=mailid
0 1 * * * /root/mysql.sh

Remove cron:

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

(Image Source Google)