Linux Basics: 20 Useful Crontab Examples in Linux

0

Cron (aka Crontab) is a task scheduler in Linux that helps to execute a task on a scheduled time, and it is very similar to Windows Task Schedulers.

With crontab, we can schedule repetitive tasks as well as one-time tasks using @ utility.

Crontab is mostly used for executing backup scripts for taking the system backups and also sometimes to start and stop applications.

Crontab

Install Crontab

The package name of crontab in CentOS / RHEL is cronie and cron in Ubuntu / Debian. You can install crontab using the below command.

### CentOS / RHEL ###

yum -y install cronie

### Debian / Ubuntu ###

apt-get install cron

Start Crontab

If the crontab is not running, then start it using the following command.

service crond start

Edit crontab

To add or edit crontab jobs of a current logged in user, use the below command. The command will open a file editor where you can update the jobs.

crontab -e

To edit other users crontab jobs, use the below command.

crontab -e -u raj

Crontab path

System wide crontab entries are found in /etc/cron{tab,.d,.daily,.hourly,.monthly,.weekly} and individual users crontab jobs are found in /var/spool/cron/ directory.

Crontab logs

Crontab logs can be found in /var/log/cron

Crontab format / Crontab time format

Below illustration shows you the crontab time format.

Crontab Examples in Linux - Crontab Time Format
Crontab Examples in Linux – Crontab Time Format

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.

That’s All. You can also read cron manual for more information.

You might also like