What is Crontab?

The Cron daemon is a service that runs on all main distributions of Unix and Linux. Specifically designed to execute commands at a given time. These jobs commonly refered to as cronjobs are one of the essential tools in a Systems Administrators tool box.

Creating a New Crontab

A crontab file is essentially a regular file located within /var/spool/cron/crontabs/. Every user on a system has the ability to create a crontab but a crontab isn’t necessarily pre-created for every user.

$ ls -la /var/spool/cron/crontabs/
    total 8
    drwx-wx--T 2 root crontab 4096 Oct 27  2014 .
    drwxr-xr-x 3 root root    4096 Oct 21  2015 ..

Before we put any data in them crontab files are nothing special, simply an empty file with a certain set of permissions. The easiest way to create a new crontab file however is to create it with the crontab command. By using the crontab file we can have crontab create the file with all of the necessary permissions.

Example

username@workstation:~$ crontab -e

Crontab Commands

  • crontab -e Edit your crontab file, or create one if it doesn’t already exist.
  • crontab -l Display your crontab file.
  • crontab -r Remove your crontab file.
  • crontab -v Display the last time you edited your crontab file. (This option is only available on a few systems.)

Crontab Parameters

# m h dom mon dow command

The above comment which you will see in all of the examples shows the parameters available to cronjobs; below is a breakdown of what they mean.

Available Crontab Fields

Field - Full Name - Allowed Values

  • m - Minute - 0 through 59
  • h - Hour - 0 through 23
  • dom - Day of Month - 0 through 31
  • mon - Month - 0 through 12
  • dow - Day of Week - 0 through 7 (0 and 7 are both Sunday)

Or a better way to understand this is:

* * * * * command to be executed
– – – – –
| | | | |
| | | | +—– day of week (0 – 6) (Sunday=0)
| | | +——- month (1 – 12)
| | +——— day of month (1 – 31)
| +———– hour (0 – 23)
+————- min (0 – 59)

Names are allowed in some implementations of cron

These parameters are what allows the user to create scheduled jobs that run at a wide variety of times. The values allowed under each field provide the user with very fine detailed execution times. Special characters are also allowed within crontabs to allow for more flexibility.

Special Characters

Special characters are used by cron to allow users to specify a range or re-occurrence of when the job should run. Below is a list of accepted special characters within the cron schedule.

Asterisk

The Asterisk is used as a wild card.

This can be used to specify any occurrence of the field.

Example:

* * * * * /home/user/command.sh

Comma

The Comma is used when creating a list.

You can use a comma to specify 2 or more times of execution.

Example:

0,15 * * * * /home/user/command.sh

Hyphen

The Hyphen is used to specify a range.

This can be used to specify any time within this range

Example:

0-59 * * * * /home/user/command.sh

Forward Slash

The Slash is used as an interval.

This can be used with a range or wild card to run at a specified interval.

Example:

*/15 * * * * /home/user/command.sh

Example Crontabs

Most of these will have multiple examples, as most of the common crontabs have multiple methods as there are many ways of scheduling a crontab.

Every Minute of Every Day

 # m h dom mon dow command
  * * * * * /home/user/command.sh or

 # m h dom mon dow command
  0-59 0-23 0-31 0-12 0-7 /home/user/command.sh

Every 15 Minutes of Every Day

 # m h dom mon dow command
  */15 * * * * /home/user/command.sh or

 # m h dom mon dow command
  0-59/15 * * * * /home/user/command.sh or

 # m h dom mon dow command
  0,15,30,45 * * * * /home/user/command.sh

Every 5 Minutes of the 2 am hour starting at 2:03

 # m h dom mon dow command
 03-59/5 02 * * * /home/user/command.sh
 # This runs at 2:03, 2:08, 2:13, 2:18, 2:23, and so on until 2:58

Every day at midnight

 # m h dom mon dow command
 0 0 * * * /home/user/command.sh

or

 # m h dom mon dow command
 0 0 * * 0-7 /home/user/command.sh

Twice Daily

 # m h dom mon dow command
  0 */12 * * * /home/user/command.sh or

 # m h dom mon dow command
  0 0-23/12 * * * /home/user/command.sh or

 # m h dom mon dow command
  0 0,12 * * * /home/user/command.sh

Every weekday at 2 am

 # m h dom mon dow command
  0 02 * * 1-5 /home/user/command.sh

Weekends at 2 am

 # m h dom mon dow command
  0 02 * * 6,7 /home/user/command.sh or

 # m h dom mon dow command
  0 02 * * 6-7 /home/user/command.sh

Once a month on the 15th at 2 am

 # m h dom mon dow command
  0 02 15 * * /home/user/command.sh

Every 2 days at 2 am

 # m h dom mon dow command
  0 02 */2 * * /home/user/command.sh

Every 2 Months at 2 am on the 1st

 # m h dom mon dow command
  0 02 1 */2 * /home/user/command.sh

To Generate a log file

To store the cron output in a file, use the closing bracket (>) again:

10 * * * * /usr/bin/php /www/virtual/username/cron.php > /var/log/cron.log

That will rewrite the output file every time. If you would like to append the output at the end of the file instead of a complete rewrite, use double closing bracket (») instead:

10 * * * * /usr/bin/php /www/virtual/username/cron.php >> /var/log/cron.log

To Block Output

If your script is very talkative, and issues all sort of information when it executes, you’ll probably want to shut it up (unless you are starved for email messages). To do this, we need to send all the normal output to a place called “/dev/null” which is basically like a black hole. It accepts anything you dump there, but you will never see it again.

Example:

# MAILTO=""
MAILTO=email@example.com
30 11 * * * /your/directory/whatever.pl >/dev/null 2>&1

What is 2>&1 here?

Here, File descriptor 1 is the standard output (stdout). File descriptor 2 is the standard error (stderr).

echo test > afile.txt

..redirects stdout to afile.txt. This is the same as doing..

echo test 1> afile.txt

To redirect stderr, you do..

echo test 2> afile.txt

>& is the syntax to redirect a stream to another file descriptor - 0 is stdin. 1 is stdout. 2 is stderr.

You can redirect stdout to stderr by doing..

echo test 1>&2 # or echo test >&2

..or vice versa:

echo test 2>&1

So, in short.. 2> redirects stderr to an (unspecified) file, appending &1 redirects stderr to stdout

Executable Scripts

10 * * * * /usr/bin/php /www/virtual/username/hello.php

Normally you need to specify the parser at the beginning of the command as we have been doing. But there is actually a way to make your PHP scripts executable from the command line like a CGI script.

You need is to add the path to the parser as the first line of the script, like hello.php:

#!/usr/local/bin/php
<?php

echo "hello world\n";

// ...

?>

Also make sure to set proper chmod (like 755) to make the file executable.

chmod 755 hello.php
./hello.php

When you have an executable script, the cron job can be shorter like this:

10 * * * * /www/virtual/username/hello.php

What is #! in above scripts?

The shebang line(#!) in any script determines the script’s ability to be executed like an standalone executable without typing python beforehand in the terminal or when double clicking it in a file manager(when configured properly). It isn’t necessary but generally put there so when someone sees the file opened in an editor, they immediately know what they’re looking at. However, which shebang line you use IS important; Correct usage is:

#!/usr/bin/env python

#!/usr/bin/env python Usually defaults to python 2.7.latest, and the following defaults to 3.latest

#!/usr/bin/env python3

How to run multiple scripts at same time

you can use article “&” for parallel work and “&&” step-by-step launch script

#script.sh & script2.sh & script.sh