Skip to content

Use cron to schedule scripts

Cron is a service available on all servers for scheduling execution of commands.

Each system user has their own crontab which is a list of scheduled commands.

A scheduled command is sometimes called a cron job.

View crontabs

View the crontab of the user you are logged in as, run the following command.

Terminal window
crontab -l

Edit crontabs

To edit the crontab of the user you are logged in as, run the following command.

Terminal window
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.

Any line in a crontab that starts with a hash (#) is a comment and will be ignored. There are usually commented lines at the top of a user’s crontab with instructions.

# 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

Add your scheduled commands below the comments.

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

Crontab format

Each line in a crontab contains two parts:

  1. The schedule of when the command will be run.
  2. The command to run.

Basic cron schedules

Cron supports the following simplified schedule formats.

  • @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 — After the server reboots.

For example, the following will run a cron job at the beginning of each hour.

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

Advanced cron schedules

Cron jobs can be scheduled to execute at specific times and specific days of the week.

For example, the following will run a 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" >>/srv/users/SYSUSER/apps/APPNAME/cron.log 2>&1

Learn more about the cron schedule format.

Log command output

To log output from commands executed by cron, redirect the command’s stdout and stderr to a log file.

To redirect a command’s output to a log file, add the follow after the command:

>>/srv/users/SYSUSER/apps/APPNAME/cron.log 2>&1

For example:

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

Debug cron commands

To debug cron jobs that are not working as intended, do the following.

  • Check the cron service logs to verify cron is running the command at the expected times.
  • Check the log output of the command.
  • Run the command manually while SSH’d into the server as the same system user the cron jobs is scheduled to run as.

Run PHP scripts with cron

Unless you know a PHP script is intended to be run using the PHP CLI, the script may only work as intended when executed through a web request.

Use the wget or curl commands in your cron job to request a PHP script rather than using the php command to execute the script.

# Good: works the same as if requested through your browser.
@hourly wget -q -O - "http://example.com/cron.php"
# Bad: may not work if the script was not written to be run with the PHP CLI.
@hourly php8.4-sp /srv/users/SYSUSER/apps/APPNAME/public/cron.php