Managing Cron jobs

From ULYSSIS documentation
Revision as of 23:33, 27 February 2017 by Yoa (talk | contribs)

Cron is a job scheduler, a utility that can be used to execute shell commands at specific points in time. These jobs are also called Cron jobs, and can be edited using the Crontab command.

The Crontab Command

To edit the cron table (crontab) file, you will use the Crontab command. The Crontab command has a few options:

  • crontab -e: Edit the crontab for the current user
  • crontab -l: List the cron jobs for the current user
  • crontab -r: Remove all cron jobs for the current user (Use the -i option to prompt before deleting)

Additionally, the -u parameter can be used to specify a user.

Editing The Crontab

Perform these steps to edit the crontab.

  • Execute the crontab -e
  • If this is the first time editing the crontab, a prompt will appear asking you to specify your text editor. It is recommended to use nano.
  • If everything is performed correctly, a file should appear.
  • You can edit this file. (Remember that everything after # is a comment)

To add a new line to the file, you should follow this format:

┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of month (1 - 31)
│ │ │ ┌───────────── month (1 - 12)
│ │ │ │ ┌───────────── day of week (0 - 6) (Sunday to Saturday;
│ │ │ │ │                                       7 is also Sunday)
│ │ │ │ │
│ │ │ │ │
* * * * *  command to execute

For example:

0 * * * * python somescript.py

will run python somescript.py every hour.

1 0 15 * 0 somecommand

will run somecommand every month the 15th day or if the day is a sunday, at 1 past midnight (00:01).

Practical Example: Scheduling Drush Updates

Drush and Cron can be used to update Drupal automatically.

  • First, run the crontab -e command. More info can be found in the previous section.
  • Next, add the following line to the file:
0 0 * * * drush pm-update http://your.website.url

This will execute the drush pm-update http://your.website.url command every day, at midnight. You can use the -l parameter for multi-site installs: drush pm-update -l http://your.website.url

  • Finally, save and exit the file and you're good to go!