Scheduling CRON Jobs (via SSH)¶
If you have access to a server via SSH, you can schedule tasks under your user accounts without having to contact technical support. User crontabs in Debian and Ubuntu are located in /var/spool/cron/crontabs, the filename is the name of user. Do not edit these files directly, but use the 'crontab -e' command.
Each line in the file equals to one Cron task. Each record consists of start time specification and the command to run. The start time configured by five columns with the following meaning:
The values are given in numbers. If you want the task to run every unit of time for the given column (for example every minute, for the
45 1 * * * uptime > /home/user/uptime.txt
If you only wanted to run this command on the fifth of the month:
45 1 5 * * uptime > /home/user/uptime.txt
There is also a special notation "asterisk/number", such as "*/5", which indicates "every 5". Therefore, in the first column, every five minutes. Of course, you can combine this with previous entries. The following entry will run the scheduled task every five minutes, but only on the fifth day of the month:
*/5 * 5 * * uptime > /home/user/uptime.txt
It is also possible to use a comma-separated list and a hyphen-separated interval. If you want to run task on the first, fifth and tenth day of the month, and only in December, always at 10 am to 10 pm, every five minutes, the entry shall be as follows:
*/5 10-22 1,5,10 12 * uptime > /home/user/uptime.txt
The last column, which has not yet been mentioned in the examples, indicates the numerical value of the day of the week. Monday is represented by number one, Sunday by number seven (or zero). So if you want to run a command every Monday, Wednesday and Friday at 12:00, the entry will look like this:
0 12 * * 1,3,5 uptime > /home/user/uptime.txt
Typical use-case for Cron on a managed server is calling a specific URL at a certain time, typically at night, to run scripts for database maintenance or data import/export. The following examples will focus on this use. The basic form of the command, where both standard and error output are redirected to /dev/null (discarded):
30 1 * * * curl http://www.example.com/scripts/maintenance.php > /dev/null 2>&1
If you call HTTPS URL, but the certificate is incorrect or not trusted:
30 1 * * * curl --insecure https://www.example.com/scripts/import.php > /dev/null 2>&1
If you plan to run a demanding task that could slow down the response or reduce the availability of applications running on the server, it is advisable to run it with a lower priority.
30 0 1 * * nice -n 19 tar -zcvf /home/user/all_domains.tar.gz /var/www
The number 19 represents the lowest priority. By default, the process starts with priority 0, the higher the number you specify, the lower the process priority.
If command execution is not limiting for the processor, but the command will cause insufficient throughput when reading / writing to the hard disks, I/O priority for this process can be reduced using ionice:
30 0 1 * * ionice -c3 tar -zcvf /home/user/all_domains.tar.gz /var/www
A value of 3 sets the process the lowest priority for I/O. The default value is 0 (no value set), the highest priority is 1. Of course, the nice and ionice commands can be combined, but the command executred this way can take a really long time to finish.
If you need to limit the running time of a script, it is possible to set a timeout (in seconds), after which the process will be hard-terminated (if still running):
30 0 1 * * timeout 3600 tar -zcvf /home/user/all_domains.tar.gz /var/www