How to set Cron on Linux?



Simple open the crontab on editor using following command:

shell> crontab -e

An editor usually vim would open. Now you can write your cron scripts that are to be schedulled there.

If I am setting cron for drupal, it would be something like

0 */1 * * * /usr/bin/wget -O - -q http://www.mysite.com/cron.php

This means the script would run every hour.

If I want to run the script every 2 hours, it would be like this:

0 */2 * * *<script>


If I want the script to execute every 30 minutes, cron would be:

30 * * * * <script>

Thus, this means * * * * * stands for minutes(0-59), hour(0-23), day of month(1-31), month(1-12), day of week(0-7 values where 0 and 7 stands for Sun or names like sun) respectively.

Therefore, if I want to run the script daily at 10pm:

0 22 * * * <script>

Thus,

 0 0 1 1 * echo "Run once a year"
0 0 1 * * echo "Run once a month"
0 0 * * 0  echo "Run once a week"
0 0 * * * echo "Run once a day"
0 * * * * echo "Run once an hour"
0 22 * * 1-5  echo "run at 10 pm on weekdays" 
5 4 * * sun     echo "run at 5 after 4 every sunday" 
5 0 * * *	echo "run five minutes after midnight, every day"

Thus, exit vim using standard :wq, your cron script is saved and your scripts would run as scheduled above.


COMMENTS