What is the Cron job?
Cron job are used to schedule commands to be executed periodically. You can setup commands or scripts, which will repeatedly run at a set time. Cron is one of the most useful tool in Linux or UNIX like operating systems. The cron service (daemon) runs in the background and constantly checks the /etc/crontab file, and /etc/cron.*/ directories. It also checks the /var/spool/cron/ directory.
What is the Crontab?
Each user can have their own crontab file, and though these are files in /var/spool/cron/crontabs, they are not intended to be edited directly. You need to use crontab command for editing or setting up your own cron jobs.
To see your crontab file, type the following command at the UNIX / Linux shell prompt:
$ crontab -l
In order to edit your crontab file, type the following command at the UNIX / Linux shell prompt:
$ crontab -e
The syntax is
1 2 3 4 5 /path/to/command arg1 arg2
1: Minute (0-59)
2: Hours (0-23)
3: Day (0-31)
4: Month (0-12 [12 == December])
5: Day of the week(0-7 [7 or 0 == sunday])
/path/to/command: Script or command name to schedule
arg1 arg2: arguments to script or command
In other word:
* * * * * command to be executed
– – – – –
| | | | |
| | | | —– Day of week (0 – 7) (Sunday=0 or 7)
| | | ——- Month (1 – 12)
| | ——— Day of month (1 – 31)
| ———– Hour (0 – 23)
————- Minute (0 – 59)
# Execute a cron job every 1 Minutes */1 * * * * /usr/local/zend/bin/php /home/ekoca/cron/ProcessOrders.php -m test
How do I use operators?
An operator allows you to specifying multiple values in a field. There are three operators:
The asterisk (*) : This operator specifies all possible values for a field. For example, an asterisk in the hour time field would be equivalent to every hour or an asterisk in the month field would be equivalent to every month.
The comma (,) : This operator specifies a list of values, for example: “1,5,10,15,20, 25”.
The dash (-) : This operator specifies a range of values, for example: “5-15” days , which is equivalent to typing “5,6,7,8,9,….,13,14,15” using the comma operator.
The separator (/) : This operator specifies a step value, for example: “0-23/” can be used in the hours field to specify command execution every other hour. Steps are also permitted after an asterisk, so if you want to say every two hours, just use */2.
How do I disable email output?
By default the output of a command or a script (if any produced), will be email to your local email account. To stop receiving email output from crontab you need to append >/dev/null 2>&1. For example:
0 3 * * * /ekoca/command.sh >/dev/null 2>&1 # OR */1 * * * * env bash /home/ekoca/cron/Rename.sh &> /dev/null
To mail output to particular email account let us say email@example.com you need to define MAILTO variable as follows:
MAILTO="firstname.lastname@example.org" 0 3 * * * /ekoca/script.sh >/dev/null 2>&1
How to debug cron commands
By default cron will mail any output from the command to the user it is running the command as. If there is no output there will be no mail. If you want cron to send mail to a different account then you can set the MAILTO environment variable in the crontab file e.g.
1 2 * * * /path/to/your/command
Then, capture the output yourself:
# which captures stdout and stderr to /log/command.log 1 2 * * * /path/to/your/command &>/log/command.log
Look at the logs; cron logs its actions via syslog, which (depending on your setup) often go to /var/log/cron or /var/log/syslog.
If required you can filter the cron statements with:
grep CRON /var/log/syslog
How to check that cron is running
If cron isn’t running then your commands won’t be scheduled …
ps -ef | grep cron | grep -v grep
you should get something like:
root 1224 1 0 Nov16 ? 00:00:03 cron
root 2018 1 0 Nov14 ? 00:00:06 crond
If not restart it:
/sbin/service cron start
/sbin/service crond start
The last command in my crontab doesn’t run, why?
Cron generally requires that commands are terminated with a new line. Edit your crontab; go to the end of the line which contains the last command and insert a new line by pressing the enter key.
#To run /path/to/command five minutes after midnight, every day, enter: 5 0 * * * /path/to/command #Run /path/to/script.sh at 2:15pm on the first of every month, enter: 15 14 1 * * /path/to/script.sh #Run /scripts/command.php at 10 pm on weekdays, enter: 0 22 * * 1-5 /scripts/command.php #Run /ekoca/scripts/script.pl at 23 minutes after midnight, 2am, 4am ..., everyday, enter: 23 0-23/2 * * * /ekoca/scripts/script.pl #Run /path/to/unixcommand at 5 after 4 every Sunday, enter: 5 4 * * sun /path/to/command #Every minute. This will run at 12:00, 12:01, 12:02, etc * * * * * /path/to/command #Once every 2 minutes. This will run at 12:00, 12:02, 12:04, etc */2 * * * * /path/to/command #Once every 5 minutes. This will run at 12:00, 12:05, 12:10, etc */5 * * * * /path/to/command #Once every 10 minutes. This will run at 12:00, 12:10, 12:20, etc */10 * * * * /path/to/command #Once every 10 minutes. This will run at 12:00, 12:10, 12:20, etc 0,10,20,30,40,50 * * * * /path/to/command #Once an hour. This will run at 12:00, 1:00, etc 0 * * * * /path/to/command #At 15 minutes past an hour. This will run at 12:15, 1:15, etc 15 * * * * /path/to/command #Once a day. This will run at 12:00 (midnight) 0 0 * * * /path/to/command #Everyday at specified time. This will run at 12:30 am 30 0 * * * /path/to/command #Once a week. This will run the cron at 12:30am every Monday 30 0 * * 1 /path/to/command #On weekdays. This will run at 10:30pm every weekday (Mon-Fri) 30 22 * * 1-5 /path/to/command #On weekdays. This will run at 10:30pm every weekday (Mon-Fri) 30 22 * * 1,2,3,4,5 /path/to/command #Specific hours. This will run at 12am, 2am, 6am, 12 noon, 8pm and 11pm everyday 0 0,2,6,12,20,23 * * * /path/to/command #Once a year. This will run at 12am (midnight) on 1st Jan 0 0 1 1 * /path/to/command #Specific dates. This will run at 12:30 am on 1st, 10th and 20th of every month 30 0 1,10,20 * * /path/to/command #Weekdays in specific month. This will run at 12 noon every weekday (Mon-Fri) only in June. 0 12 * 6 1-5 /path/to/command #Once in 6 month. This will run at midnight on 1st Jan and 1st June 0 0 1 1,6 * /path/to/command