Linux - Crontab

Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at specified times

Commands

  • crontab -e - Edit your crontab file, or create one if it doesn’t already exist.
  • crontab -l - Display your crontab file.
  • crontab -r - Remove your crontab file.
  • crontab -v - Display the last time you edited your crontab file (This option is only available on a few systems.)
 1crontab -e
 230 18 * * * [command] 
 3
 430 18 * * * rm /home/someuser/tmp/* > /home/someuser/cronlogs/clean_tmp_dir.log
 5
 6
 7*     *     *   *    *        command to be executed
 8-     -     -   -    -
 9|     |     |   |    |
10|     |     |   |    +----- day of week (0 - 6) (Sunday=0)
11|     |     |   +------- month (1 - 12)
12|     |     +--------- day of month (1 - 31)
13|     +----------- hour (0 - 23)
14+------------- min (0 - 59)
15
16min	hour	day/month       month	day/week  Execution time
1730	0	1	        1,6,12	*	  – 00:30 Hrs  on 1st of Jan, June & Dec.
180	20	*	        10	1-5	  – 8.00 PM every weekday (Mon-Fri) only in Oct.
190	0	1,10,15	        *	* 	  – midnight on 1st ,10th & 15th of month
205,10	0	10	        *	1	  – At 12.05,12.10 every Monday & on 10th of every month

Notes

Repeat pattern like /2 for every 2 minutes or /10 for every 10 minutes is not supported by all operating systems.

The specification of days can be made in two fields: month day and weekday. If both are specified in an entry, they are cumulative meaning both of the entries will get executed .

Crontab Example

A line in crontab file like below removes the tmp files from /home/someuser/tmp each day at 6:30 PM.

130     18     *     *     *   rm /home/someuser/tmp/*

Disable Email

By default cron jobs sends a email to the user account executing the cronjob. If this is not needed put the following command At the end of the cron job line .

130 18 * * * rm /home/someuser/tmp/* > /dev/null 2>&1

Generate log file

To collect the cron execution execution log in a file :

130 18 * * * rm /home/someuser/tmp/* > /home/someuser/cronlogs/clean_tmp_dir.log

run the drupal cron process every hour of every day

10 * * * * /usr/bin/wget -O - -q -t 1 http://localhost/cron.php

run this apache kludge every minute of every day

1* * * * * /var/www/devdaily.com/bin/check-apache.sh
15 10,22 * * * /var/www/devdaily.com/bin/mk-new-links.php

run the backup scripts at 4:30am

130 4 * * * /var/www/devdaily.com/bin/create-all-backups.sh

re-generate the blog "categories" list (four times a day)

15 0,4,10,16 * * * /var/www/devdaily.com/bin/create-cat-list.sh

reset the contact form just after midnight

15 0 * * * /var/www/devdaily.com/bin/resetContactForm.sh

rotate the ad banners every five minutes

10,20,40  * * * * /var/www/bin/ads/freshMint.sh
25,25,45  * * * * /var/www/bin/ads/greenTaffy.sh
310,30,50 * * * * /var/www/bin/ads/raspberry.sh
415,35,55 * * * * /var/www/bin/ads/robinsEgg.sh

https://github.com/alseambusher/crontab-ui