Crontab in Linux with Examples

If you found this post helpful, kindly share it and show your support :)

The crontab could be a list of commands that you just need to run on a regular schedule. Crontab stands for “cron table”. The cron is that the system process which will automatically perform tasks for you in keeping with a set schedule.

Linux Crontab Format

MIN HOUR DOM MON DOW CMD

Crontab Fields and Allowed Ranges (Linux Crontab Syntax)

Field    Description    Allowed Value
MIN      Minute field    0 to 59
HOUR     Hour field      0 to 23
DOM      Day of Month    1-31
MON      Month field     1-12
DOW      Day Of Week     0-6
CMD      Command         Any command to be executed.

To List all Active Cron Jobs Running, you can run below command

crontab -l

To list cron jobs that belong to a particular user, run the following command:

sudo crontab -u username -l

Replace username with the actual username that you want to list cron jobs

Examples of Cron jobs

1.Scheduling a Cron job For a Specific Time

For Eg :- To execute the Full backup shell script (full-backup) on 10th May 08:30 PM

The time field uses 24 hours format. So, for 8 PM use 20 (for 8 AM use 08)

30 20 10 05 * /home/testuser/full-backup

30 – 30th Minute , 20 – 08 PM, 10 – 10th Day, 05 – 5th Month (May) and * – Every day of the week

2. To schedule a Cronjob for every minute

* * * * *  command-that-you-want-to-execute
  • * all the possible unit means every minute of every hour through out the year.
  • */5 in minute field means every 5 minutes.
  • 0-10/2 in minute field mean every 2 minutes in the first 10 minute.

3. To schedule a Cronjob for more than one time (e.g. Twice a Day)

For Eg :- To take a incremental backup twice a day every day (10:00 and 15:00 on every day)

00 10,15 * * * /home/testuser/incremental-backup

00 – 0th Minute (Top of the hour), 10,15 – 10 AM and 3 PM, * – Every day, * – Every month and * – Every day of the week

4.To schedule a Cronjob for certain range of time (e.g. Only on Weekdays)

For eg :-

a) To checks the status of a service everyday (including weekends) during the working hours 10 a.m – 7 p.m

00 10-19 * * * /home/testuser/check-service-status

00 – 0th Minute (Top of the hour), 10-19 – 10 am, 11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm, 7 pm, * – Every day, * – Every month and * – Every day of the week

b) To checks the status of a service every weekday (i.e excluding Sat and Sun) during the working hours 10 a.m – 7 p.m.

00 10-19 * * 1-5 /home/testuser/check-service-status

00 – 0th Minute (Top of the hour), 10-19 – 10 am, 11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm, 7 pm, * – Every day, * – Every month and 1-5 -Mon, Tue, Wed, Thu and Fri (Every Weekday)

5. To schedule a Cron job for every 10 minutes.

For Eg:- To check the disk space usage every 10 minutes.

*/10 * * * * /home/testuser/check-disk-space

Cron special keywords and its meaning

Keyword    Equivalent
@yearly    0 0 1 1 *
@daily     0 0 * * *
@hourly    0 * * * *
@reboot    Run at startup

1. To schedule a Cronjob for first minute of every year using @yearly

@yearly /home/testuser/annual-maintenance-check

This will execute the scheduled task at 00:00 on Jan 1st for every year.

2. To schedule a Cronjob beginning of every month using @monthly

@monthly /home/testuser/take-backup

This will execute the scheduled task at 00:00 on 1st of every month.

3. To schedule a Cronjob every day using @daily

@daily /home/testuser/cleanup-logs "day started"

This will execute the scheduled task at 00:00 on every day.

4. To execute a linux command after every reboot using @reboot

@reboot command

This will execute the given command once after the server got booted every time.

Loading

Mohammed Noufal

I'm Mohammed Noufal, working as Server Admin for the last 10 years.  In my day-to-day life, l had to face different problems related to Web-hosting. On my website Errorlogz.com, you can find solutions for different Web-hosting-related problems. Sometimes, I spent hours searching/googling to find a solution. This inspired me to start a website that gives solutions to different Webhosting problems. This website includes basic Linux and windows commands, and different control panels like cPanel, Plesk, DirectAdmin, Webmin & so on. You can find solutions and or suggestions for different Web-hosting related problems from here. Hence I would like to say Errorlogz is your server protector.  I will be glad if Logdetect can help any one of the Server admins to find a solution to his problem.

You may also like...

Leave a Reply

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