Schedule tasks with cron jobs

cron, schedule, php, wp-cli, curl, email notice · 0 found this helpful

A cron job runs a command at set times. Use it for backups, imports, cleanup, or WordPress scheduled tasks.

Add a cron job

  1. Open Account → Hosting → your site → Developer.
  2. Under Scheduled tasks, find New scheduled task.
  3. Under How often, pick a setting such as Every hour or Every day, then the time, or choose Custom and fill in the five time boxes in cron format. The summary below shows when it will run.
  4. Enter the Command.
  5. Click Add task.
  6. To get the output by email, enter an address under Email task output to and click Save. It applies to every task. Otherwise send output to /dev/null in the command.
Scheduled tasks, with the New scheduled task form

Schedule syntax

┌ minute (0-59)
│ ┌ hour (0-23)
│ │ ┌ day of month (1-31)
│ │ │ ┌ month (1-12)
│ │ │ │ ┌ day of week (0-6, Sunday is 0)
* * * * *  command
Pattern Runs
* * * * * Every minute. Allowed, but use it sparingly, since every 5 or 15 minutes is enough for most jobs. Set it with Custom in How often
*/15 * * * * Every 15 minutes
0 * * * * Every hour, on the hour
30 2 * * * Every day at 2:30 a.m.
0 9 * * 1-5 Weekdays at 9 a.m.
0 0 1 * * First day of each month at midnight
0 3 * * 0 Sundays at 3 a.m.

Times use the server's time zone, which is UTC, not yours. For example, 30 2 * * * runs at 2:30 a.m. UTC, which is 10:30 p.m. Atlantic Standard Time. Check the server time with date.

Examples

# Run a PHP script daily at 2:30
30 2 * * * /usr/local/bin/php /home/yourusername/scripts/cleanup.php >/dev/null 2>&1

# WordPress scheduled tasks every 10 minutes (set DISABLE_WP_CRON in wp-config.php)
*/10 * * * * cd /home/yourusername/public_html && /usr/local/bin/wp cron event run --due-now >/dev/null 2>&1

# Call a URL
0 6 * * * /usr/bin/curl -fsS https://example.ca/cron.php?key=SECRET >/dev/null 2>&1

# Export the database daily, keep 7 days
15 1 * * * mysqldump -u yourusername_shop -p'PASSWORD' --single-transaction yourusername_bakery | gzip > /home/yourusername/backups/db-$(date +\%F).sql.gz && find /home/yourusername/backups -name 'db-*.sql.gz' -mtime +7 -delete

# Node.js script
0 4 * * * /home/yourusername/nodevenv/app/20/bin/node /home/yourusername/app/job.js >> /home/yourusername/logs/job.log 2>&1

/usr/local/bin/php runs the account default PHP version, not a site's own choice under Performance → Choose PHP for each site. To use another version, give its full path, such as /opt/alt/php83/usr/bin/php. For Node.js, use the path to your app's node under ~/nodevenv/.

In a crontab, % must be written as \%. Always use full paths.

Tips

  • Redirect output to a log file instead of email. >> ~/logs/job.log 2>&1 appends output.
  • Don't overlap runs. Use flock: * * * * * flock -n /tmp/job.lock /path/to/script.
  • Frequent jobs use CPU. See Resource limits explained.
  • Store passwords in a config file with 600 permission, not on the cron line.

If something goes wrong

Problem Fix
Never runs Check syntax, and the path to the script
Works by hand, not in cron Cron has a smaller environment. Use full paths and cd first
Flood of emails Add >/dev/null 2>&1
Killed partway CloudLinux limits. Run it less often or break up the work
Wrong PHP version Use the full path to the PHP you want. See Change PHP version and extensions

For WordPress, see Use WP-CLI.

Was this helpful?

Still stuck? Ask us and we'll walk you through it.