Introduction to Crons
A common way to automatically run scripts at schedules times is to use a cron. For example, use a cron to make scheduled web requests to a specific URL on your website. This reliable way will perform grunt work, such as emailing users, updating stats or do XYZ productive task.
WordPress and Drupal have integrated cron-like systems that run scheduled tasks without cron. You need not configure cron jobs for WordPress or Drupal.
You have your own list of scheduled tasks. This list is called a crontab. To view a crontab, SSH in to your server and run this command:
To edit a crontab, type this command:
The first time you edit a crontab, you may be asked which editor to use. Just choose the default option. After you select your editor, you'll be editing the crontab. Look for some comments at the top of the crontab to learn from. Create your own comments by starting lines with a hash (#).
You'll see this in your editor:
# Edit this file to introduce tasks to be run by cron. # # Each task to run has to be defined through a single line # indicating with different fields when the task will be run # and what command to run for the task
To add a cron job that makes a web request every hour, please scroll to the bottom and add:
@hourly wget -q -O - "http://domain.com/cron.php"
Using the default editor, you can save your changes by pressing
Crontab Entries Formatting
Each crontab line contains two parts: how often to run the command followed by the command to run. Use this website to assist cron creation and / or read below to learn more.
How Often to Run Command
Just use any of the following for configuring a command:
Once an hour at the beginning of the hour
Once a day at midnight
Once a week at midnight on Sunday morning
Once a month at midnight on the morning of the first day of the month
Once a year at midnight on the morning of January 1
The Command to Run
The format of commands in the crontab is exactly the same as if you were running the command through SSH, yup yup, that it is.
Logging Crontab Output
You may log output for each crontab entry by redirecting command output to a log file.
For example, this will log all messages and errors to the file
@hourly wget -q -O - "http://domain.com/cron.php" >>/srv/users/userXYZ/apps/APPNAME/cron.log 2>&1
Emailing Crontab Output
You may also configure cron to email output and error messages. Just add a
MAILTO address at the top of the crontab.
MAILTO="[email protected]" @hourly wget -q -O - "http://domain.com/cron.php" >>/srv/users/userXYZ/apps/APPNAME/cron.log 2>&1
Advanced Crontab Scheduling
You may also schedule cron jobs to execute at exact times and specific days of the week. Instead of using
@daily as the time to run a command, just specify the time as the minute of the hour, the hour of the day, the day of the month, the month of the year, and the day of the week.
For example, this will run our cron job at 1:35 a.m. on the 4th and 20th day of each month.
35 1 4,20 * * wget -q -O - "http://domain.com/cron.php"
An asterisk (*****) means all values instead of a specific value.
Debugging Crontab Commands
When debugging or writing scripts to be run by cron, you can run your scripts manually through SSH. If you think your script(s) are broken, SSH into your server, inspect crontab to analyze the command cron is running, and run the same command in your shell.
Debugging is easier if you've logged each cron job's output.
Tip: PHP Cron Jobs Web Requests
A great way to run PHP scripts through cron is using web requests of the script on your site. This can be done using a
wget command, as shown in the examples above.
It's possible to use the PHP CLI to run scripts, but your scripts will not have the same environment settings when running through PHP CLI. As a result, they may not work correctly.
# Good: works as if requested through your browser. @hourly wget -q -O - "http://example.com/cron.php" # Bad: may not work at all or may have errors. @hourly php5.6-sp /srv/users/userXYZ/apps/APPNAME/public/cron.php