Advanced technical details of a crontab

Basic details

  • The crontab files are where the lists of jobs and other instructions to the cron daemon are kept. Each user at DreamHost has their own individual crontab file and there is a system wide crontab file at /etc/crontab (but this file can only be edited by a system administrator).
  • Crontab files are simple text files that have a particular format. Each line of a crontab file follows a particular format as a series of fields, separated by spaces and/or tabs. Each field can have a single value or a series of values. A single cron job should take up exactly one line, but this can be a long line (more than 80 characters).
  • Each line has five time/date fields, followed by a command, followed by a newline character (’\n’). A common problem is not including a newline, so hit 'Enter/Return' a time or three at the end of your command.
  • Another common problem is automatic word-wrap breaking up a long line into multiple lines, so make sure your text editor doesn't do this.
  • Blank lines and leading spaces and tabs are ignored. Lines whose first non-space character is a hash-sign (#) are ignored as they are considered comments. Note that comments are not allowed on the same line as cron commands, since they are interpreted as being part of the command. Similarly, comments are not allowed on the same line as environment variable settings (like MAILTO).

Date/Time fields

The first five fields of the line are the date and time field which specify how frequently and when to execute a command. When adding the cron job in the DreamHost panel, the Date/Time is added for you automatically based on your 'When to run' setting.

Field no. Description Permitted values
1 minute 0-59
2 hour 0-23
3 day of the month 1-31
4 month 1-12
5 day of the week 0-7

Note: For day of the week, both 0 and 7 are considered Sunday. The time is based on that of the server running cron.

Another (graphical) way of looking at these fields.

 # * * * * *  command to execute
 # │ │ │ │ │
 # │ │ │ │ │
 # │ │ │ │ └───── day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
 # │ │ │ └────────── month (1 - 12)
 # │ │ └─────────────── day of month (1 - 31)
 # │ └──────────────────── hour (0 - 23)
 # └───────────────────────── min (0 - 59)

There are several ways of specifying multiple values in these fields:

  • The comma (',') operator specifies a list of values.
    • 1,3,4,7,8
  • The dash ('-') operator specifies a range of values.
    • 1-6
    • This is equivalent to "1,2,3,4,5,6".
  • The asterisk ('*') operator (frequently known as a wildcard) specifies all possible values for a field. For example, an asterisk in the hour (second) field would be equivalent to 'every hour'.
  • The slash ('/') operator can be used in conjunction with an asterisk to skip a given number of values. Example:
    • /3
    • This means to skip to every third value. So "*/3" in the hour field is equivalent to "0,3,6,9,12,15,18,21"; "*" specifies 'every hour' but the "/3" means that only the first, fourth, seventh, etc. values given by "*" are used.

You can also use one of these special strings in place of the time/date fields.

Entry Description Equivalent to
@yearly (or @annually) Run once a year at midnight on January 1 0 0 1 1 *
@monthly Run once a month at midnight on the first day of the month 0 0 1 * *
@weekly Run once a week at midnight on Sunday morning 0 0 * * 0
@daily (or @midnight) Run once a day at midnight 0 0 * * *
@hourly Run once an hour at the beginning of the hour 0 * * * *
@reboot Run at startup (of the cron daemon) @reboot

Review the following Wikipedia article for further information:

Output

The output of the cron job is determined by what is sent to the terminal as a result of the commands/script that are executed. By default, all output is emailed to the location specified in the MAILTO variable (see the MAILTO variable requirement section for more information). As noted above, if your cron job command doesn't create any output on the command line then no email is sent.

You can provide special instructions for the standard out (STDOUT) and standard error (STDERR) output by using the ">" operator. When you use ">" without a number before it, it defaults to "1>". This is the standard (non-error) output.

When you use "2>" you are specifying what to do with the error output. So, for example, ">my_file.txt" would redirect standard output to a file called "my_file.txt", and "2>my_errors.txt" would redirect the errors to a file called "my_errors.txt".

Permissions

By default, files created on DreamHost's servers have a permissions level of 644. If you choose to execute a script via cron, you may need to set the permissions for the file to 744 using chmod in order to allow it to execute properly.

Examples

  • Example 1: This runs a command at 4:10 PM PST/PDT, and email you the regular and error output to the destination specified by MAILTO.
10 16 * * * /home/username/bin/yourscript.pl
  • Example 2: This runs a command at 2:00 AM PST/PDT on Saturday, and the only output is errors.
0 2 * * 6 /home/username/weekly/weekly-pruning.sh > /dev/null

 

  • Example 3: This runs at midnight on New Years Day (January 1st), and there is no output.
0 0 1 1 0 /home/you/happy.new.years.pyc >/dev/null 2>&1

2>&1 is a special redirect that sends the standard error (“2>”) output to the same place as the standard out (“>” or “1>”) output.

  • Example 4: This runs a PHP script called cron.php at the top of every hour.
0 * * * * wget -O /dev/null http://www.example.com/cron.php
  • Example 5: This runs a local script (i.e. hosted at DreamHost) every 15 minutes.
*15 * * * * /usr/local/bin/perl /home/username/example.com/myscript.pl
  • Example 6: This runs an external script (i.e. hosted elsewhere) every 30 minutes using curl.
*30 * * * * /usr/bin/curl -s http://example.com/send.php &> /dev/null

&>/dev/null is an abbreviation for 1> /dev/null 2> &1. It redirects both file descriptor 2 (STDERR) and descriptor 1 (STDOUT) to /dev/null.

View http://unix.stackexchange.com/a/70971 for more information.

Manually editing crontab

To edit the your crontab manually you'll have to log in to your server via SSH. You can replace your existing crontab with a file you have uploaded using the following command:

[server]$ crontab /home/username/filename

You can also directly edit your crontab by executing the following command:

[server]$ crontab -e

Once you've saved your changes and exited the text editor, you can check that the change was successful using the following command:

[server]$ crontab -l
  • To remove your crontab and start fresh:
[server]$ crontab -r

Dedicated server editing

If you are logged in as a Dedicated server admin user, you can edit the crontab file directly. It is stored here:

/var/spool/cron/crontabs/youruser

You’ll need to use sudo on your Dedicated server (or start an interactive session as the root user with sudo -i) to access that file. 

If you require sudo/admin access, you must upgrade to a Dedicated server.

Example (opening the file with the 'vi' text editor):

[server]$ sudo vi /var/spool/cron/crontabs/youruser

See also

Did this article answer your questions?

Article last updated .