Knowledgebase Home | Glossary | Favorites Knowledgebase Home | Glossary | Favorites
Create a scheduled job with cron
Article Details

Last Updated
25th of April, 2011

Level
intermediate

User Opinions (0 votes)
No users have voted.

How would you rate this answer?
Helpful
Not helpful

Prerequisites:

Connecting to your Container via SSH
Vim Basics

Related Articles:

Syncing files between multiple contianers.
Cron enables users to schedule jobs (commands or shell scripts) to run automatically at a certain time or date. It is commonly used to automate system maintenance or administration. Some CMS like Drupal use cron to automate some tasks. Learn more.

To edit what jobs are scheduled, first SSH to your container, then run:
crontab -e

This should open up an empty crontab if this is your first time using Cron.



Cron uses the following syntax to schedule jobs.
minute hour day month day-of-week command-line-to-execute

Some examples of proper scheduling syntax:

*/5 * * * * : Run cron every 5 minutes
* */1 * * * : Run cron every hour
* * */3 * * : Run cron every 3 days
* * * */1 * : Run cron once every month
*/5 * */3 * * : Run cron every 5 minutes, all day, every 3rd day.
30 0 * * * : Run cron at 12:30 every day

An example of a cron job which will remove all the contents of a cache directory every 3 days:

* * */3 * * rm -rf /home/stacys/sites/stacyssite.com_Ver1/doc_root/cache/*

To list all cronjobs in your crontab:

crontab -l 

Note:

If your cronjob is to run a php script, crond never assumes a Current Working Directory. Your cronjob needs to change directories to the location of the script before running if the cron script creates a relative include path. An example of how to run a cronjob which calls a php script would be as follows:

30 0 * * * * cd /home/username/sites/www.site.com_ver1/doc_root/cron/ && /usr/bin/php cronscript.php

By default, any error messages are logged in /var/log/messages

Visitor Comments
  1. Comment #1 (Posted by Elric)
    God help me, I put aside a whole afternoon to fgriue this out.
  2. Comment #2 (Posted by Fanni)
    Good to see a tleant at work. I can't match that.
  3. Comment #3 (Posted by Ryne)
    I was really cnoufesd, and this answered all my questions.
Post Comment for "Create a scheduled job with cron"
To post a comment for this article, simply complete the form below. Fields marked with an asterisk are required.
   Your Name:
* Your Comment:
* Enter the code below:
 
Related Articles
No related articles were found.
Attachments
No attachments were found.

Continue