Advertisement Area

15 Useful Crontab Examples in Linux

15 Useful Crontab Examples in Linux

Cron, is a task scheduler in Linux which helps to perform a task on scheduled time, and it is very similar to Windows Task Schedulers. With crontab, we can schedule respective tasks as well as one-time tasks.

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

How to install Crontab

yum -y install cronie

Start Crontab

If the crontab is not running automatically, then use the following command to start.

service crond start

Edit crontab

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

crontab -e

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

crontab -e -u user1

The 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 can be found in /var/log/cron
Crontab format / Crontab time format

The below image shows you the crontab time format.

15 Useful Crontab Examples in Linux - Crontab Time Format

15 Useful Crontab Examples in Linux – Crontab Time Format

Let’s see some useful Crontab examples

Let us start with basic crontab example.

1. Schedule a crontab at particular time (at 2 AM)

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

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

This cron command will run everyday at 2AM.

2. Schedule a cron to run twice a day

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

0 9,21 * * * /path/to/script.sh

This cron runs twice a day at 9 AM and 9 PM.

3. Schedule a cron to run every Sunday 1 AM

If you 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 to be Sunday.

4. Schedule a crontab every minute

Sometimes you may require run cron for every minute. You should take care that this cron does’t consumes more memory while running every minute in the server (You can avoid this, if you feel it degrades the server performance)

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

5. Schedule a crontab every 5 minutes

If you may need to run a program like pinging servers for their availability(like checking uptime).

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

6. Schedule a crontab every hour

Below cron 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, to run a script every two hours.

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

8. Schedule a crontab daily (daily cron)

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

The below cron example used 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 Wednesday and Friday at 2 PM.

0 14 * * wed,fri /path/to/script.sh

11. Schedule a crontab every week (weekly cron)

To schedule a quick weekly cron job use the 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

If you want a cron job to be executed on 15th of every month, You can use the below example

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

This 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

To run script or command after every reboot use the below example.

@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="techadmin"
1 1 * * * /path/to/script.sh

17. Change shell in cron

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

If you 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

To set the home directory to use when executing scripts.

HOME=/
1 1 * * * /path/to/script.sh

We came to end of the post, Hope this helps your need. Please share your valuable comments to improve us.

Note : The cron cannot be used to schedule a job based on every second interval.

For more information, you can read cron manual

To know about Xen virtualization, please click here.

You may also like...

0

  1. Malik techie guy says:

    Thanks, useful information about cron job

  2. Ilayaperumal says:

    Nice one

  3. Manikandan says:

    Good job admin,very useful information.

  4. ulaganathan says:

    Useful information, thanks for sharing your knowledge
    its great ooh useful to know the csf commands.

Leave a Reply

Your email address will not be published. Required fields are marked *

Advertisement Area