How do I create a cron job?

Overview

You can have DreamHost automatically configure a cron job in the panel on the Cron Jobs page. This article contains the following contents.

When creating the cron job, you must assign it to a shell user in your panel. Only shell users are able to run cron jobs.

Cron jobs can be created on any hosting plan.

Creating a cron job in the panel

This example runs a single file named script.php.

  1. Navigate to the Cron Jobs page.
    The 'Cron Jobs' page appears:
  2. Click the Add New Cron Job button.
    The 'Creating New Cron Job' page opens:
  3. To create the cron, fill in the following fields:
    • User: From the dropdown menu choose an existing shell user you'd like the cron job to run under.
    • Title: Give the cron job a name you'll remember.
    • Email output to: Enter an email address you'd like to receive the cron output to. You can also leave this blank to not receive emails.
    • Command to run: Enter the command to run.

      The command you enter usually must first specify the path to the program you'd like to use to run the cron job. View the 'Paths to common programs' below for examples. For example let's say you have a PHP file named script.php in your domains directory:

      • /home/username/example.com/script.php

      To run this command you'd enter the path to your chosen version of PHP followed by a space, followed by the path to the file:

      • /usr/local/php74/bin/php /home/username/example.com/script.php
    • Use locking: Enabling this if you want to prevent the job from running more than once at a time.
    • When to run: From the dropdown menu, choose how often you'd like this job to execute.

      The time you choose should be PDT/PST since this is the timezone your webserver is set to. To figure out the correct time you can either log into your server via SSH and run the command date, or you can check a website that lists this information.

      Custom minutes

      It's currently only possible to add 12 different minutes within an hour. You can arrange these how you like, but the cron can run no more than 12 times within a single hour.

The 'Day of Week' and 'Day of Month' fields

When the 'Day of Week' field overrides the 'Day of Month'

The Day of Week field will override the Day of Month field only if the Day of Month field is set to Every Day. For example, the following configuration would only run on Thursdays.

crontab day of week

When the 'Day of Week' field does NOT override the 'Day of Month'

The Day of Week field will NOT override the Day of Month field if the Day of Month field is set to a specific day(s). They will then run together.

In the example below, the Day of Week field is set to run at 3:44 pm Thursday. The Day of Month field is also set for the 26th (which is a Wednesday). The cron job will still run every Thursday, but also on the 26th of the month. 

crontab day of week

Running a cron job from a URL with variables

It's common for a custom URL in your site to use variables. For example:

https://example.com/script.php?firstname=john&lastname=doe

The problem is that the variables only work in a web browser when visiting the URL. They will not function when running the URL on the server. There are two ways you can have a cron job run such a URL.

Using curl or wget

When running curl or wget on a URL with variables, make sure to enclose the URL in single quotes.

curl 'https://example.com/script.php?firstname=john&lastname=doe'

Here's how it would look in the panel.

cron variables using curl

Using php.cgi

The other option is to use php.cgi instead of the path to PHP. When creating the URL, you must change the first ? sign into a space, and every & after that into a space. For example:

Path to original file and URL

/home/username/example.com/script.php?firstname=john&lastname=doe

Edited path to file and URL

/home/username/example.com/script.php firstname=john lastname=doe

Finally, add the path to the php.cgi binary.

/dh/cgi-system/php74.cgi /home/username/example.com/script.php firstname=john lastname=doe

Here's how it would look in the panel.

cron variables using php.cgi

Paths to common programs

Below are the paths to common programs on the server. This is the path you would add before the file you wish to run.

Program Path
bash /bin/bash
java /usr/bin/java
perl /usr/bin/perl
php /usr/bin/php
/usr/local/php72/bin/php
/usr/local/php73/bin/php
/usr/local/php74/bin/php
php-cgi /dh/cgi-system/php72.cgi
/dh/cgi-system/php73.cgi
/dh/cgi-system/php74.cgi
python /usr/bin/python
/usr/bin/python3
ruby /usr/bin/ruby

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?