Crontab Usage

From Brian Nelson Ramblings
Jump to: navigation, search

How to create and use Crontabs to Manage a Server

The cron daemon is a long running process that executes commands at specific dates and times. To schedule one-time only tasks with cron, use at or batch. For commands that need to be executed repeatedly (e.g. hourly, daily or weekly), use crontab, which has the following options:

[email protected] 	Emails the output to the specified address.
crontab -e 	Edit your crontab file.
crontab -l 	Show your crontab file.
crontab -r 	Remove your crontab file.

Crontab Structure

The crontab command creates a crontab file containing commands and how often cron should execute them. Each entry in a crontab file consists of six fields, specified in the following order:

minute(s) hour(s) day(s) month(s) weekday(s) command(s) 

The fields are separated by spaces or tabs. The first five are integer patterns and the sixth is the command to be executed. The following table briefly describes each of the fields:

Field Value Description

minute  0-59 	The exact minute that the command sequence executes.
hour 	 0-23 	The hour of the day that the command sequence executes.
day 	 1-31 	The day of the month that the command sequence executes.
month 	 1-12 	The month of the year that the command sequence executes.
weekday 0-6 	The day of the week that the command sequence executes. Sunday=0, Monday = 1, Tuesday = 2, and so forth.
command Special 	The complete command sequence variable that is to be executed.

Each of the patterns from the first five fields may either be an asterisk (*) (meaning all legal values) or a list of elements separated by commas. An element is either a number or two numbers separated by a minus sign (meaning an inclusive range). Note that the specification of days may be made by two fields (day of the month and day of the week). If both are specified as a list of elements, both are followed. For example:

   [email protected]
   0 0 1,15 * 1 /usr/bin/php /path/to/your/php/script.php

The cron daemon would run the program script.php on the first and fifteenth of each month, as well as on every Monday. To specify days by only one field, the other field should be set to *. For example:

   [email protected]
   0 0 * * 1 /usr/bin/php /path/to/your/php/script.php 

The program would then only run on Mondays.

Learn more about Crontabs

If a cron job specified in your crontab entry produces any error messages when it runs, they will be reported to you via email.

For more information, consult the man pages. man pages are the directions and tutorials available to you right at the command line. Type any of the following lines to open the relevant tutorials ([Enter] means to hit the Enter (return) key):

   man 5 crontab [Enter]
   man 1 crontab [Enter]
   man cron [Enter]
   man at [Enter]
   man batch [Enter]
   man 1 cron [Enter] 

Removing/Stopping the Cron

Deleting the cronfile.txt file from your account will not stop the cron. You may remove this file at any time, however since the server's crontab already has the contents, the cron will still run once it has been made active.

To turn the cron off, you must connect to your account via SSH and issue the following command:

   crontab -r 

The crontab -r will deactivate the cronjob and remove the file contents from the server.