How to schedule tasks in GNU/Linux

By GNU_and_Tux.svg: Tux.svg: Larry Ewing, Simon Budig, Anja GerwinskiHeckert_GNU_white.svg: Aurelio A. Heckert Gnulinux.png: Aurelio A. Heckert , (using The GIMP), Dudufderivative work: Wondigoma (talk) - GNU_and_Tux.svgGnulinux.png, FAL, Alarm Clock Vector.svg from Wikimedia Commons by, CC-BY-SA 4.0
Mouse over to view credits
cron is a time-based job scheduler in Unix-like computer operating systems, cron is used to schedule jobs at fixed times, dates, or intervals; cron runs every minute and detects if there is a new command or task (reading the files /etc/crontab, /etc/cron.d/* and /var/spool/cron/crontabs/*) to run.

crontab is the program that maintains: installs, uninstalls or lists the scheduled tasks, each user has his own crontab file in /var/spool/cron/crontabs/username which should not be edited directly else using the crontab utility. If the /etc/cron.allow exists, your user must appear in it to be able to schedule tasks, if it does not exist and /etc/cron.deny exists your user must not appear in it; if neither of them exist then it depends on the default OS configuration.

crontab is also the file that contains instructions for executing scheduled tasks. A line in the crontab file can set an environment variable (var-name=value) or schedule the execution of a command; blank lines and lines beginning with # (comments) are ignored. The format for scheduling a task is:

# (0-59) (0-23)    (1-31)             (1-12)       (0 - 7)
    min   hour   day-of-the-month      month    day-of-the-week	       command
  • – * = all posibles values
  • – value1-value2 defines a range of values
  • – value1,value3,value3 defines a list
  • – value1-value2/step defines a step inside the range
  • – 0 or 7 = Sunday in the day of the week


Make a backup of my home DIR

Send my home DIR last changes to a remote server every 5 minutes between 9:00 a.m – 6:00 p.m

*/5 9-18 * * * rsync -avz $HOME remote-server:/mnt/backup

Read 16 Practical examples of the rsync command for sync behaviour.

Backup my DB

Make a backup to my databases every day and every hour.

0 */1 * * * mysqldump -u root -pclave --all-databases > $HOME/bds.sql

Send a reminder

Email a remainder on the 1st and 15th of each month at 2:15 p.m

15 14 1,15 * * mail -s 'Recordatorio' < /dev/null

Backup my projects

Send my projects last changes to a remote server every 2 hours between 8:30 a.m – 5:30 p.m

30 8-17/2 * * 1-5  rsync -avz $HOME/prt servidor:/mnt/backup/prt

Read 16 Practical examples of the rsync command for sync behaviour.

All together

# These are enviroment variables
# Use for command execution
# Cron will send notification to this address.
MAILTO="Put your email here"
*/5 9-18 * * * rsync -avz $HOME remote-server:/mnt/backup
0 */1 * * * mysqldump -u root -pclave --all-databases > $HOME/bds.sql
15 14 1,15 * * mail -s 'Recordatorio' < /dev/null
30 8-17/2 * * 1-5  rsync -avz $HOME/prt servidor:/mnt/backup/prt

To edit your crontab file execute this command

crontab -e

Further readings

– man cron
– man 1 crontab
– man 5 crontab

Spanish Video

YouTube video

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.