Administration
...
Background tasks
Configuring Cron for Linux
Crontab basics
7min
cron is a system scheduler used in unix like operating systems it is used to automatically run scripts, commands, or programs at specified times and dates a scheduled task is known as a cron job and is defined in a file called crontab (cron table) crontab commands to edit the crontab file, view cron jobs, display the contents of the current crontab or delete it, enter the following commands in the terminal crontab e — edit the crontab this command will create a new crontab if one does not already exist and will open it in the default text editor crontab u username e — edit another user’s crontab file to edit other users’ files, you need superuser privileges use sudo su before running the command crontab l — view cron jobs and display the contents of the current crontab file crontab u username l — view the crontab files list for another user run this command with superuser privileges crontab r — delete the current crontab file of the current user crontab i — this command performs the same function as crontab r, but prompts for confirmation before deleting the crontab crontab syntax the crontab file consists of cron jobs (commands) a cron job is divided into two parts a cron expression followed by the command you want to execute here’s an example of a cron job /bin/sh backup sh the first five fields specify the time when the cron job will be executed each field is represented by an asterisk ( ) and specifies the timing for the task an asterisk means every possible value for that time unit each field can have a single value, a range of values, a list of values, or a special character the fields are separated by spaces or tabs time unit description range minutes the minute of the hour when the command will be launched 0 59 hours the hour when the command execution will start 0 23 day of month the day of the month on which you want to run the command 1 31 month the month in which the command will be executed 1 12 day of week the day of the week on which you want to execute the command 0 6 (sunday is 0) /bin/sh backup sh means running a backup every minute 30 18 rm /home/sydtesting/tmp/ means that files in /home/sydtesting/tmp will be deleted every day at 18 30 additionally, it is important to use the special characters character description example asterisk ( ) represents "every" possible value for the field it is used in in the day of week field means every day of the week comma (,) used to list multiple values within a single field 5,15,45 in the minutes field means the job will run at the 5th minute, the 15th minute, and the 45th minute of every hour hyphen ( ) specifies a range of values 1 5 in the day of week field means the job will run from monday to friday slash (/) indicates increments or step values /5 in the minutes field means the job will run every 5 minutes (0, 5, 10, 15, etc ) last (l) used in the day of month and day of week fields to represent last 0 0 l would run the job at midnight on the last day of every month weekday (w) specifies the nearest weekday to a given day of the month 15w in the day of month field means the job will run on the closest weekday to the 15th of the month hash (#) used in the day of week field to specify the nth occurrence of a weekday in a month 2#3 means the third monday of the month question mark (?) used in the day of month and day of week fields as a placeholder for no specific value it is often used when one of these fields is specified and the other is left as any value 0 0 15 8 ? means "run at midnight on the 15th of august, regardless of the day of the week" email notifications after each cron job is executed, cron automatically sends an email notification to the user this notification includes the result of the job or any error messages that occurred if you do not want to receive these emails, just add >/dev/null 2>&1 to the syntax, as shown in the example 0 5 /root/backup sh >/dev/null 2>&1 if you want the output to be sent to a different email address, you can add a mailto line at the beginning of the crontab file with the desired email address for example mailto="my name\@my host com" 0 3 /root/backup sh >/dev/null 2>&1 cron job examples expression meaning 0 0 /bin/sh backup sh perform database backup at midnight once a day 0 6,18 /bin/sh backup sh perform db backup twice a day at 6 am and 6pm 0 /6 /scripts/monitor sh perform monitoring every six hours /10 /home/user/script sh perform cron job for the script file located in the home directory every 10 minutes 0 20 7 /bin/sh backup sh launch database backup every hour every 20th of july 0 0 2 /bin/sh launch db backup at midnight every tuesday 1,2,5 /script/script sh execute the command in january, february, and may 10 59/5 5 /home/user/script sh execute the command every 5 minutes at 5 am, starting at 5 10am 0 8 1 /3 /home/user/script sh launch the command quarterly on the 1st day at 8am /scripts/script sh; /scripts/scrit2 sh schedule multiple tasks in one cron job @reboot /scripts/script sh perform a task each time the system is started 0 0 1 /home/user/script sh execute the command on the first day of each month