Creating a custom Cron Job

 
 

Overview

This article explains how to manually create a custom cron job via SSH on your DreamHost server.

Prerequisites

This article assumes you have already Created a Shell user and are able to log into your server via SSH.

Terminology

Cron job: A cron job is a command that runs a specific script at a specific time. These can be created in the panel or manually on the server.

Crontab file: A crontab file stores all cron jobs for a single user on the server.

How custom and panel cron jobs interact

The following two options are available to customize your user's cron jobs or crontab file.

Any changes/updates to a user's cron jobs in the panel will overwrite any customizations added to the crontab file.

If you choose to customize your crontab file, you must no longer use the panel's cron service for the specific user.

Editing a cron job created in the panel

These instructions can be used to edit an existing cron job you created in the panel. However, this is NOT recommended since any updates to the user's cron jobs in the panel will overwrite your custom crontab file.

If you created a cron job in the panel, you should only continue to edit it from the panel.

Switching the server's crontab file with your own

You can use a custom crontab file created under your username to replace the default crontab file on the server. If you do this, the server's crontab is overwritten with your custom file and any cron jobs created in the panel for this user will no longer function.

This is NOT recommended since any updates to the user's cron jobs in the panel will overwrite your custom crontab file.

Run the following command to use your custom crontab file named mycrontab:

[server]$ crontab /home/username/mycrontab

Editing on a Dedicated Server

If you are logged in as an admin user on a Dedicated Server, you can edit the crontab file directly using sudo -i. The crontab file is located in the following directory:

/var/spool/cron/crontabs/username

DreamPress websites

The instructions in this article do not apply to DreamPress websites. If you need to create a cron job for a DreamPress website, it must be created in the panel. View this article for further instructions.

Contents of a crontab file

The following describes the different sections of a crontab file. 

Viewing a crontab file

Each user at DreamHost has their own individual crontab file. To view this file, log into your server via SSH and run the following command:

[server]$ crontab -l

The contents will appear similar to the following if you've previously created a cron job in the panel:

###--- BEGIN DREAMHOST BLOCK
###--- Changes made to this part of the file WILL be destroyed!
# Title
MAILTO="admin@example.com"
30 8 1 * 3 /usr/bin/setlock -n /tmp/cronlock.3790180184.244762 sh -c 'sh /home/username/file.sh'
###--- You can make changes below the next line and they will be preserved!
###--- END DREAMHOST BLOCK

Title

This is what you named the cron job in the panel.

MAILTO

This is the email any output is sent to. You will only receive an email of the cron job's output if the script it executes writes data to the terminal. 

If the script the cron job executes does not create any output on the command line, there is no output to email.

/tmp/cronlock

This appears if you've checked Use locking when creating a cron job in the panel.

Date/Time fields

The first five values are the date and time fields, which specify when to execute a command. All cron jobs run based on the time of DreamHost web servers which are set to PST.

Fields

Field no. Description Permitted values
1 minute 0-59
2 hour 0-23
3 day of the month 1-31
4 month 1-12
5 day of the week 0-7 (both 0 and 7 are considered Sunday)

Graphical view of these fields

 * * * * *  command to execute
 │ │ │ │ │
 │ │ │ │ │
 │ │ │ │ └───── day of week (0 - 6 are Sunday to Saturday)
 │ │ │ └────────── month (1 - 12)
 │ │ └─────────────── day of month (1 - 31)
 │ └──────────────────── hour (0 - 23)
 └───────────────────────── min (0 - 59)

Specifying multiple values

Value Description Example
Comma , specifies a list of values

1,3,4,7,8

Dash -

specifies a range of values

1-6 is equivalent to "1,2,3,4,5,6"
Asterisk * specifies all possible values for a field An asterisk in the hour (second) field would be equivalent to 'every hour'.
Slash / can be used in conjunction with an asterisk to skip a given number of values

/3 means to skip to every third value.

So, */3 in the hour field is equivalent to "0,3,6,9,12,15,18,21"

Special strings in place of the time/date fields

Value Description Equivalent to Example
@yearly or @annually Run once a year at midnight on January 1 0 0 1 1 * @yearly php /home/username/mail.php
@monthly Run once a month, at midnight, on the first day of the month 0 0 1 * * @monthly php /home/username/mail.php
@weekly Run once a week, at midnight, on Sunday 0 0 * * 0 @weekly php /home/username/mail.php
@daily or @midnight Run once a day at midnight 0 0 * * * @daily php /home/username/mail.php
@hourly Run once an hour at the beginning of the hour 0 * * * * @hourly php /home/username/mail.php
@reboot Run at startup (of the cron daemon) @reboot @reboot php /home/username/mail.php

See the following Wikipedia article for further information:

Creating a custom cron job

The following steps explain how to add a custom cron job under your Shell user.

These instructions assume you have not added a cron job in the panel yet, so the crontab file is blank. If you see content when editing this file, stop and review the section above since any updates to the panel will overwrite your customizations. 

The following section is also available as an instructional video:

Creating a cron job

  1. Log into your server via SSH using the Shell user you wish to create the cron job under.
  2. Run the following command to open your crontab file.
    [server]$ crontab -e
    It looks like you don't have a MAILTO line in your crontab file
    For performance reasons we ask that you specify an address where
    cronjob output will be delivered.  If you do not wish to receive
    cronjob output, simply press enter and cronjob output will not be
    mailed to you.
    
    For more information regarding this, please visit:
    https://help.dreamhost.com/hc/en-us/articles/215088608
    
    Where would you like cronjob output delivered? (leave blank to disable)
    : admin@example.com
    
    cronjob output will be emailed to admin@example.com
    confirm? (y/N): y
    
  3. Type the letter y and press Enter to proceed.
    Select an editor.  To change later, run 'select-editor'.
      1. /usr/bin/joe
      2. /usr/bin/jstar
      3. /usr/bin/jpico
      4. /usr/bin/jmacs
      5. /usr/bin/jed
      6. /bin/nano        <---- easiest
      7. /usr/bin/vim.basic
      8. /usr/bin/rjoe
      9. /usr/bin/mcedit
      10. /usr/bin/vim.tiny
      11. /bin/elvis-tiny
      12. /usr/bin/emacs
      13. /bin/ed
    
    Choose 1-13 [3]: 6
    
  4. Choose an editor to view this file. #6 uses the program nano which is the easiest option.
  5. Add the code for your cron job in the blank crontab file that opens. For example:
    # Custom cron job
    MAILTO="user@example.com"
    13 20 * * * php  /home/username/mail.php
    This runs a file named mail.php at 8:13 pm under the username of username. This should be the same SSH username you're currently logged in under.

    There are two parts to the command.

    • the path to the program to run the script
    • the path to the script

    This example uses a path to a version of PHP. You could also use the default version by using php instead of the full path.

    /usr/local/php83/bin/php

    Follow this with a space.

    Then, add the path to the file:

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

    The full version would look similar to this:

    /usr/local/php83/bin/php /home/username/example.com/script.php
  6. Save the file. You should see the following response:
    crontab: installing new crontab

The cron job should now run every day at 8:13pm.

Things to look out for when editing/creating your crontab

newline character

Each line has five time/date fields, followed by a command, followed by a newline character ('\n').

A common problem is not including a newline, so make sure to press Enter at the end of your command a few times.

Ignored content

Blank lines, leading spaces, and tabs are ignored.

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?