Help

« Back

Cron

The Cron deamon is a service that allows you to run scripts and programs at specific dates and times. The commands to be executed are stored in a table. This table is called crontab. This article will show you how to access the crontab by means of SSH and how to run scripts and programs at defined times.

To use Cron, you must establish an SSH connection to your project. Then, enter the crontab -e command to open the crontab file.

Note: The crontab file is located in the /var/spool/cron directory.

Editing the crontab file using vi

The vi editor will open by default when calling crontab -e.

Note: To edit the crontab file using Nano editor, you can optionally enter the EDITOR=nano crontab -e command.

Vi has an insert mode and a command mode. You can open the insert mode using the i key. The characters entered will immediately be inserted in the text in this mode. To switch to command mode, press the ESC key. When you are in command mode, key presses will be understood as commands.

Note: Vi cannot be stopped in insert mode. For that reason, you must always switch to command mode to exit vi.

The following pages list the most important commands:

Command Description
dd Delete the current line
u Undo the last change
J Summarize lines
o Insert a new line under the current line
P Paste copied data at the cursor position
:q! Close the editor without saving changes.
:wq Save the text and close the editor

Structure of the crontab file and crontab syntax

All commands to be executed by cron are stored in a table. This table is called a crontab and consists of the following columns:

Minutes (0 - 59) Hours (0 - 23, 0 = Midnight) Days (1 - 31) Month (1 - 12) Weekday (0 - 6, 0 = Sunday) Command or directory and name of the script file
* * * * * for example /var/www/directory/myscript.sh

The first 5 columns in this table will contain time information. The entries in these columns may not be spaces. The 6th column will contain the command or the directory and name of the script file. Spaces will separate the individual columns.

Attention: When entering a command, make sure to enter the complete path. Also, be aware that a hard carriage return (such as '\n') must always follow the command.

In the following example, the /var/www/html/sites directory should be backed up on January 1 and every Monday in January at 4:01 a.m.:

01 04 1 1 1 cp -a /var/www/html/sites /var/www/html/backups

The backup will be saved to the /var/www/html/backups directory.

To back up the /var/www/html/sites directory daily at 4:01 a.m., enter the following command:

01 04 * * * cp -a /var/www/html/sites /var/www/html/backups

If the directory should be backed up every 10 minutes, enter the following command:

*/10 * * * cp -a /var/www/html/sites /var/www/html/backups

To execute the command at 8 a.m., 9 a.m. 10 a.m., enter the command in the following format:

* 8-10 * * cp -a /var/www/html/sites /var/www/html/backups
Note: Optionally, you can enter the time using the following format: 8,9,10

To back up the /var/www/html/sites directory from January to June at 4:01 a.m., 4:31 a.m., 5:01 a.m. and 5:31 a.m. daily between the 1st and the 15th of the month, enter the following command:

01,31 04,05 1-15 1,6 * cp -a /var/www/html/sites /var/www/html/backups

Using pre-defined strings for specify the time

You can optionally use pre-defined strings to specify the time for a cron job. The following pages list these strings:

String Meaning Replaces the following entry in the crontab file
@reboot once during system startup
@yearly once yearly 0 0 1 1 *
@annually once a year 0 0 1 1 *
@monthly once a month 0 0 1 * *
@weekly once a week 0 0 * * 0
@daily once a day 0 0 * * *
@midnight once a day 0 0 * * *
@hourly once an hour 0 * * * *

Executing scripts using cron

You can create a script to execute several commands or programs in combination. You can then schedule the execution of the script via cron. The following pages describe how to create a script and a crontab entry by way of example:

  1. Open the editor.
  2. Add the script in the following format:

    #!/bin/bash cp -a /var/www/html/sites /var/www/html/backups cp -a /var/www/html/index.php /var/www/html/backups 
  3. Save the script to the /var/www/html/cronscripts directory. Give the file an appropriate file name.

  4. Enter the following command to execute the script without specifying the interpreter:

    chmod +x /var/www/html/cronscripts/backup.sh
  5. Enter the crontab -e command.
  6. Make an entry in the crontab file.

    01 04 * * * /var/www/html/cronscripts/backup.sh
  7. To switch to command mode, press the ESC key.
  8. Enter the :wq command to save your changes.

Displaying the list of created cron jobs

The following command lets you display a list of all cron jobs that have been created:

crontab -l

Log files

All of the cron actions will be entered in the $CRON_LOG_FILE log file. In most cases, this file is located in the /var/spool/cron directory.

Notifications

Using the MAILTO variable will send you an email notification if an error occurs during the execution of a cron job. This variable is listed in the crontab file. If you enter an e-mail address, the recipient will be notified about all actions executed by cron.