The Cron deamon is a service that allows you to run scripts and programs at scheduled times. The commands to be executed are stored in a table called the crontab. This article explains how to access the crontab via SSH and how to run scripts and programs at scheduled times.
To use Cron, you need to create a connection to your project via SSH. To open the crontab file, enter the command crontab -e.
You can find the crontab file under the /var/spool/cron directory.
If you choose to edit the crontab file with the Nano editor, enter the command EDITOR=nano crontab -e.
Vi has both an insert mode and a command mode. Press i to open the insert mode. The characters entered will be directly added to the text. To switch to command mode, press ESC . When you are in command mode, any keys pressed will be understood as commands.
To exit vi, always switch from insert mode to command mode first.
The most important commands are listed below:
|dd||Delete current line|
|u||Undo last change|
|o||Insert new line under the current line|
|P||Paste copied data at the cursor position|
|:q!||Close editor without saving changes|
|:wq||Save text and close editor|
Structure of the crontab file and crontab syntax
All commands to be executed by cron are listed in a table. This table is called the 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|
|*||*||*||*||*||z. B. /var/www/directory/myscript.sh|
The first 5 columns in this table contain information about the time. You cannot use the space key in the entries of these columns. The 6th column contains the command or the directory and name of the script file. The columns are seperated with blank spaces.
When entering a command, make sure to enter the complete path. Remember to always follow the command with a hard line break (such as '\n').
In the next example, the /var/www/html/sites directory should be backed up on January 1st at 4:01 a.m., then again every following Monday in January at the same time:
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
To back up the directory 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. and 10 a.m., enter the command in the following format:
* 8-10 * * * cp -a /var/www/html/sites /var/www/html/backups
You can also enter the time in this 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 from the 1st till the 15th of each 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 to specify the time
You can also use pre-defined strings to specify the time for a cron job. The strings are listed below:
|STRING||MEANING||REPLACES THE FOLLOWING ENTRY IN THE CRONTAB FILE|
|@reboot||once during system startup|
|@yearly||once a year||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 combined. You can then schedule the execution of the script via cron. The following example explains how to create a script and a crontab entry:
Open the editor.
Enter the script in the following format:
cp -a /var/www/html/sites /var/www/html/backups
cp -a /var/www/html/index.php /var/www/html/backups
Save the script in the /var/www/html/cronscripts directory and name the file.
Enter the following command to execute the script without specifying the interpreter:
chmod +x /var/www/html/cronscripts/backup.sh
Enter the command crontab -e.
Add an entry to the crontab table.
01 04 * * * /var/www/html/cronscripts/backup.sh
To switch to command mode, press ESC.
Enter the command :wq to save changes.