How To add Cron Jobs to your Servebolt site

Cron jobs are used to run scripts on scheduled times on the server. Cron jobs are configurable in the control panel. If a tech contact is set for your Bolts, any errors or output from the cron jobs will be sent to the tech contacts email.

Good cron tips

  • Run scripts directly using PHP or sh, instead of triggering scripts over HTTP (by using wget, curl etc).
  • Run cron jobs as seldom as possible. This saves resources and reduces the workload on the server and databases.
  • If you run multiple cron jobs, make sure they do not overlap (use sleep for instance, see examples below).
  • Test your crons manually to make sure they’re working as intended with
    wp cron event run --all --path=/kunder/user_6258/site_19172/public
    (this is an example path, check your Bolt and site settings for the correct path).

Adding cron jobs

Time is specified in cron syntax. The following example runs the cron.php script in the public folder every fifth minute:
*/5 * * * * /usr/bin/php ~/public/cron.php

Add a second cron job that runs at the same intervals , but waits 30 seconds before it starts.
*/5 * * * * /usr/bin/sleep 30; /usr/bin/php ~/public/cron_job_2.php

Paths to normal commands:
/usr/bin/php
/usr/bin/sh
/usr/bin/wget

If you need help in figuring out the cron schedule, this resource should be very helpful.

Errors and Debugging in Cron Jobs

When cron jobs produce error output, an email will be sent to the Bolt’s Technical Contact, if that is set for your Bolt. In addition, cron logs any error output to a cron.log file.

The most common reason for PHP based cron jobs is a memory limits. The PHP memory limit can be adjusted by calling your PHP script with the -d parameter like this: php -d memory_limit=512M script.php 

If there are other errors, the way to debug cron is to log in via SSH and execute the scripts. Then any error output will be posted directly to screen.

Give us your feedback on this article