Crontab Job Scheduling

Crontab Job Scheduling

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

Edit/Add Crontab

#crontab -e# crontab -u user_name -e

List Crontab Jobs

# crontab -l# crontab -u user_name -lExamples

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.