1. Home
  2. cPanel/WHM
  3. How do I set up cron jobs?

How do I set up cron jobs?

Getting started with cron

Via cPanel, you can find the cron settings via the icon Cron Jobs under Advanced.

Choose when the cron job should be run and enter the Command you wish to run, then click Add New Cron Job.

You have to enter the command in the following fashion:

/path/to/program /home/yourusername/path_to_script > /dev/null 2>&1

For example, if you want to run a php script, the command could look like this:

/etc/cl.selector/php /home/yourusername/script.php > /dev/null 2>&1

If you want to run a specific PHP version in the cronjob, enter /opt/alt/php73/usr/bin/php instead of /etc/cl.selector/php.

You may choose any installed PHP version by swapping out 73 in the command above for any other PHP version (e.g. 70 if you want to use PHP 7.0).

Using a URL in a cronjob

If your website is running a system that requires cronjobs that are run via URLs, you cannot simply enter the URL as the cron command to run. Instead, you need to tell the system to use the command wget. This will tell the server to visit the entered URL, in the same way as if you were to visit it via your web browser. Hence, enter the command for your cronjob as follows:

wget -O /dev/null -o /dev/null "https://www.exempel.se/command"

Swap out https://www.exempel.se/command for the proper URL needed to run your cronjob. Make sure that you keep the quotation marks around the URL, otherwise it may not work.

Managing output

If you wish, you can choose that an email should be sent to you after each cron job run. This can be practical if you want to see whether or not the job completed successfully. It may also help you troubleshoot a cron job that is not functioning as expected – the mail may then contain helpful error messages. If you want to receive those emails, you have to remove “> /dev/null 2>&1” from the cron job command:

/etc/cl.selector/php /home/yourusername/path_to_script.php

You can select the receiving email address in the Email field just above where you set the time for your cron job.

Please note! If you want to run wget via cron and don’t wish any output, you must set both the -o and -O flags to /dev/null, for example:

wget -O /dev/null -o /dev/null "https://www.exempel.se"

Overlapping cron jobs

A problem that may arise when running cron jobs is that you don’t know exactly how long each run of the command will take. If you have a command that runs every five minutes and finishes in three, all is fine; but if you have a command that runs every five minutes and requires seven minutes to finish you run into trouble. In practicality, this will result in multiple cron jobs running at once – something you want to avoid.

There is, however, a solution: Flock. Flock creates a lock file when the cron job starts, and if the file still exists the next time the cron job is supposed to start, it won’t. When the cron job that created the lock file is done, the file is removed automatically.

You can use Flock in the following way:

/usr/bin/flock -w 1 /tmp/cron.lock /etc/cl.selector/php -q /home/yourusername/path_to_script.php

/usr/bin/flock is the path to the Flock binary, and /tmp/cron.lock the path to the lock file – you do not have to edit those. You enter the rest of the cron job command after the lock file path, as described above.

Was this article helpful?

Related Articles