Articles

How to Use Cron to Schedule Scripts

Introduction

The most common way to automatically run scripts at scheduled times is through cron. For example, you can use cron to make scheduled web requests to a specific URL on your website. This is a reliable way to perform periodic work, such as mailing users or generating reports.

WordPress and Drupal have implemented systems that will initiate background processing of their scheduled tasks without cron. You do not need to configure cron jobs for WordPress and Drupal.

Viewing Crontabs

Each system user has their own list of scheduled tasks. This list is called a crontab. To view a crontab, SSH in to your server and run the command:

crontab -l

Editing Crontabs

To edit a system user's crontab, run the command:

crontab -e

The first time you edit a crontab, you'll be asked which editor to use. You should choose the default option unless you prefer a different editor.

Once you select your editor, you'll be editing the crontab. There are some comments at the top of the crontab. You can create your own comments by starting lines with a hash (#).

This is what you'll see 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, scroll to the bottom of the file and add the line:

@hourly wget -q -O - "http://example.com/cron.php"

If you've used the default editor, you can save your changes by pressing CTRL-O followed by Enter. To exit, press CTRL-X.

The Format of Crontab Entries

Each line in the crontab contains two parts: how often to run the command followed by the command to run.

How Often to Run the Command

You can use any of the following for configuring how often to run a command:

@hourly
Once an hour at the beginning of the hour
@daily
Once a day at midnight
@weekly
Once a week at midnight on Sunday morning
@monthly
Once a month at midnight on the morning of the first day of the month
@yearly
Once a year at midnight on the morning of January 1
@reboot
At startup

The Command to Run

The format of commands in the crontab is exactly the same as if you were running the command through SSH.

Logging Output from Crontab

You can log output from each crontab entry by redirecting command output to a log file.

For example, the following will log all messages and errors to the file /srv/users/serverpilot/apps/APPNAME/cron.log.

@hourly wget -q -O - "http://example.com/cron.php" >>/srv/users/serverpilot/apps/APPNAME/cron.log 2>&1

Emailing Output from Crontab

You can also configure cron to email output and error messages to you. To do this, add a MAILTO address at the top of the crontab.

MAILTO="you@example.com"
@hourly wget -q -O - "http://example.com/cron.php" >>/srv/users/serverpilot/apps/APPNAME/cron.log 2>&1

Advanced Crontab Scheduling

You can also schedule cron jobs to execute at specific times and specific days of the week. To do this, instead of using @hourly or @daily as the time to run a command, you can 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, the following will run our cron job at 1:35 a.m. on the 7th and 20th day of each month.

35 1 7,20 * * wget -q -O - "http://example.com/cron.php"

An asterisk (*) means all values rather than a specific value.

Debugging Crontab Commands

When writing or debugging scripts to be run by cron, you can always run your scripts manually through SSH.

If you think your script isn't working, SSH into your server, view your crontab to see the exact command cron is running, and run the same command in your shell.

Debugging will also be easier if you've logged each cron job's output.

Tip: Use Web Requests for PHP Cron Jobs

The best way to run PHP scripts through cron is using web requests to the full URL of the script on your website. This can be done using the wget command, as shown in the examples above.

Although it is possible to use the PHP CLI to run scripts, your scripts will not have the same environment settings when run through the 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/serverpilot/apps/APPNAME/public/cron.php