Managing Cron jobs: Difference between revisions
(Created page with "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 Cro...") |
No edit summary |
||
(9 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
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 | 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= | ==The Crontab Command== | ||
To edit the cron table (crontab) file, you will use the | To edit the cron table (crontab) file, you will use the crontab command. The crontab command has a few options: | ||
* <code>crontab -e</code>: Edit the crontab for the current user | * <code>crontab -e</code>: Edit the crontab for the current user | ||
* <code>crontab -l</code>: List the cron jobs for the current user | * <code>crontab -l</code>: List the cron jobs for the current user | ||
* <code>crontab -r</code>: Remove all cron jobs for the current user (Use the -i option to prompt before deleting) | * <code>crontab -r</code>: Remove all cron jobs for the current user (Use the -i option to prompt before deleting) | ||
===Editing The Crontab=== | |||
==Editing The Crontab== | |||
Perform these steps to edit the crontab. | Perform these steps to edit the crontab. | ||
* Execute the <code>crontab -e</code> | * Execute the <code>crontab -e</code> command | ||
* If this is the first time editing the crontab, a prompt will appear asking you to specify your text editor. | * If this is the first time editing the crontab, a prompt will appear asking you to specify your text editor. If you don't know what to reply, we suggest you choose <code>nano</code>. | ||
* If everything is performed correctly, a file should appear. | * If everything is performed correctly, a file should appear. | ||
* You can edit this file. (Remember that everything after # is a comment) | * You can edit this file. (Remember that everything after # is a comment) | ||
Line 28: | Line 26: | ||
* * * * * command to execute | * * * * * command to execute | ||
You can use an asterisk (*) if a field does not matter. | |||
For example: | For example: | ||
<code>0 * * * * python somescript.py</code> | <code>0 * * * * python somescript.py</code> | ||
Line 33: | Line 32: | ||
<code>1 0 15 * 0 somecommand</code> | <code>1 0 15 * 0 somecommand</code> | ||
will run <code>somecommand</code> every month the 15th day '''or if the day is a | will run <code>somecommand</code> every month the 15th day '''or''' if the day is a Sunday, at 1 past midnight (00:01). | ||
= | ==Real-life Example: Scheduling Drush Updates== | ||
Drush and Cron can be used to update Drupal automatically. | Drush and Cron can be used to update Drupal automatically. | ||
* First, run the <code>crontab -e</code> command. More info can be found in the previous section. | * First, run the <code>crontab -e</code> command. More info can be found in the previous section. | ||
* Next, add the following line to the file: | * Next, add the following line to the file: | ||
<code>0 0 * * * drush pm-update | <code>0 0 * * * cd /path/to/your/drupal/folder/ && drush pm-update</code> | ||
This will execute the <code> | This will execute the <code>cd /path/to/your/drupal/folder/</code> command, changing the current directory to your drupal folder, and the <code>drush pm-update</code> command, updating Drupal, every day, at midnight. The <code>&&</code> indicates a new command follows. | ||
* Finally, save and exit the file and you're good to go! | * Finally, save and exit the file and you're good to go! | ||
[[Category:Shell]] |
Latest revision as of 13:25, 1 November 2020
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 usercrontab -l
: List the cron jobs for the current usercrontab -r
: Remove all cron jobs for the current user (Use the -i option to prompt before deleting)
Editing The Crontab
Perform these steps to edit the crontab.
- Execute the
crontab -e
command - If this is the first time editing the crontab, a prompt will appear asking you to specify your text editor. If you don't know what to reply, we suggest you choose
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
You can use an asterisk (*) if a field does not matter. 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).
Real-life 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 * * * cd /path/to/your/drupal/folder/ && drush pm-update
This will execute the cd /path/to/your/drupal/folder/
command, changing the current directory to your drupal folder, and the drush pm-update
command, updating Drupal, every day, at midnight. The &&
indicates a new command follows.
- Finally, save and exit the file and you're good to go!