1. Home
  2. Authoring and Development
  3. What is Cron and How do I Use It?

What is Cron and How do I Use It?

The system utility cron can be used to schedule programs to run automatically at predetermined intervals.

Advanced users can use cron via the Unix shell or the Account Control Center (ACC) to easily set up and maintain a schedule of programs to be run.

Please note that you should not use both the Unix shell and the ACC to manage cron. If you make changes to cron via the Unix shell, all future changes to that cron command will not show up in the ACC cron interface. Any attempts to edit the command in the ACC will not take effect.

What is Cron?

Cron is a standard Unix utility that is used to schedule commands for automatic execution at specific intervals. For instance, you might have a script that produces web statistics that you want to run once a day automatically at 5:00 AM.

Commands involving cron are referred to as "cron jobs."

Cron in the Account Control Center

The Account Control Center (ACC) has a built-in interface for cron. We recommend that all customers use the ACC cron interface, although advanced users may use cron manually.

To find the ACC cron interface, follow these steps:

  1. Log in to the Account Control Center (ACC) at my.pair.com
  2. In the left sidebar, click Advanced
  3. In the drop-down, click Manage Cron Jobs

This will take you to the Cron Job Configuration interface where you will be able to create, edit, and delete cron jobs.

If this is your first time visiting the Manage Cron Jobs page, you may need to Enable Cron Jobs before accessing the rest of the cron interface. 

The table at the bottom of the page contains currently configured cron jobs.

Creating a Cron Job

Do not use both the command line and the ACC to manage cron. Using both may result in conflicts.

You can add a cron job directly from the Cron Job Configuration page. If you need help coming up with a cron command, see these resources:

To add a cron job in the Account Control Center:

  1. At the bottom of the page, click Add New Cron Jobnew cron job image
  2. Enter the cron job information:
    Command to run This is the command that the cron job will run automatically.

     

    We recommend you specify the full file path to the command. See this note for more information.

    For example, cron commands, see our Example Cron Commands section.

    Frequency Establishes how often the cron job will run
    Day of the month/Day of the week/Interval The day or time frame when the cron job will run.

     

    This section will change based on the selected frequency

    Time of day The specific time that the cron job will run. Note that the time specified is EST.

     

    This section is only used with the Once a Month, Once a Week, and Once a day frequencies

    We recommend scheduling cron jobs at different times to keep cron jobs from slowing down web performance.

  3. Click Add Cron Jobcreate new cron job image

It's important to understand that our servers are set to the Eastern time zone. This setting could affect your cron jobs during Daylight Saving Time (DST). In the United States, DST begins at 2 A.M. during the second Sunday in March and ends 2 A.M. during the first Sunday in November.

If you set a cron job to run in the DST hour "gap" (between 2 AM and 3 AM), your cron job will not run when DST begins and time jumps ahead one hour. In addition, if you have a cron job set to run in the DST hour "gap" and DST ends, your cron job will run twice.

If any errors have occurred, you will be notified by the Account Control Center immediately and given a chance to correct them.

After you have added a job, you will see it listed in the table on the Cron Job Configuration page. cron list image

Manage Existing Cron Jobs

You can edit or delete existing cron jobs from the Cron Job Configuration page. All of your cron jobs will be listed in the table at the bottom of the page.change image

To edit a cron job, find the command in the Command column and click it.

By clicking on the job's command link, you'll be taken to that cron job's management page. From this page, you can edit the job or delete it entirely.

Edit Existing Cron Job

Once on the cron job's management page, you can edit the job by changing the command listed next to Command. When finished, click Change Command. change cron image

This will change the cron job immediately. When the cron job is next scheduled to run, it will run the new command.

Delete Existing Cron Job

You can delete the job by clicking the Delete Job button. delete cron image

This will permanently delete the cron job.

Advanced Cron Usage (Manual)

To use cron manually, you must first set up a file of cron entries. This file can be placed anywhere in your account and can have any filename (though crontab.txt is a good choice).

It should contain one entry for each job you are scheduling.

For instructions on how to create a file using Unix commands, read our How to Create New File section of the Unix Basics article.

Basic Cron Entry

A cron entry looks similar to the following:

30 3 * * * /usr/bin/nice

/usr/home/username/script.pl

In this case, script.pl would be run nightly at 3:30 AM.

/usr/bin/nice utilizes the system nice command to lower the impact of the call on the server. We recommend that you use it with all your cron jobs to best preserve system resources.

/usr/home/username/script.pl is the full path to the script/program you would like cron to run.

You can also use regular Unix commands using full paths to programs. For instance, you could have the following command to remove all files from some temporary directory:

30 3 * * * /usr/bin/nice /bin/rm -f

/usr/home/username/temp/*

Establish Cron Time

The "30 3 * * *" in the example requires a bit more explaining:

30 3 * * * /usr/bin/nice /bin/rm -f

/usr/home/username/temp/*

The time a cron command is executed is controlled by the 5 numbers that precede the program call.

  • The first number is the minutes after the hour (0-59)
  • The second is the hour of the day (0-23, with 0 being 12 AM EST)
  • The third number is the day of the month (1-31)
  • The fourth is the month of the year (1-12)
  • The fifth is the day of the week (0-6, with 0 = Sunday and 6 = Saturday, etc.)
time image
This example would run at 1:15 on December 9th.

An asterisk (*) matches all possibilities. For instance, in the given example the script runs every day because the asterisks for day of the month, day of the week, and month of the year match all values.

Multiple values are separated by commas. For instance, you might start with "15 1,3,5 * * *" to run a script every day at 1:15, 3:15, and 5:15.

Note that our policies prohibit a program running via cron more often than once every hour.

Activate the Cron Command

Once you have your cron directive file in your account, you must connect via SSH and issue the command:

crontab crontab.txt

Replace crontab.txt with the actual file name of the file holding your cron entries.

Note that you must be in the same directory as the cron file at the time.

After this, your cron job should be activated. If the programs running via cron generate errors, the cron daemon will email them to your account.

If you would like to change any of your cron jobs, just edit your file, and rerun the crontab command as shown above.

Example cron Commands

The following are commands that you can use to backup your site's files. These commands can be used in the ACC cron interface's Command to run field or in the manual creation of cron jobs.

A Note on Cron Job Commands

Be sure to specify the full path to each program used in the cron job.
For instance, to use the Unix "cat" command, you should use "/bin/cat" instead of just "cat."

In another example, you may run a script in your home directory by using:
$HOME/script.pl

Keep in mind, that when you run a custom script, it must be marked as
executable or the cron job will fail.

Backup Home Directory Command

To back up your home directory (and exclude the "www_logs" and "backup" directory) use the following command:

/usr/bin/tar czf 

/usr/home/username/backup/home.`/bin/date

+\%Y\%m\%d`.tar.gz --exclude www_logs --exclude 

backup

/usr/home/username

Replace "username" with your Pair Networks username.

Backup public_html Directory Command

Accounts Created before June 11, 2011

For accounts with a separate public_html directory (typically, accounts created before June 1, 2011), the public_html directory can be backed up with:

/usr/bin/tar czf

/usr/home/username/backup/web.`/bin/date

+\%Y\%m\%d`.tar.gz

/usr/www/users/username

Replace "username" with your pair Networks username.

Accounts Created after June 11, 2011

To back up your public_ftp directory:

/usr/bin/tar czf

/usr/bin/tar czf

/usr/home/username/backup/ftp.`/bin/date

+\%Y\%m\%d`.tar.gz /usr/public_ftp/username

Replace "username" with your Pair Networks username.

Backup Web Logs Command

To back up your web logs:

/usr/bin/tar czf 

/usr/home/username/backup/logs.`/bin/date

+\%Y\%m\%d`.tar.gz /usr/home/username/www_logs

Replace "username" with your Pair Networks username.

Backup Database Command

To back up a particular database, see our automated backups setup tutorials:

Running PHP Scripts via Cron Job

It is possible to run PHP scripts via a cron job. To do so, there are three options:

Change the Starting Line of Code

One option to run PHP scripts via cron jobs is to change the starting line of the code to be a full path to a CGI copy of PHP.

For example, the <?php starting line of code could be changed to:

#!/usr/www/cgi-bin/php7.cgi -q

The file could then be called with the full path to the file.

Call PHP CLI First

Another way to run PHP scripts in the command line is by calling the PHP CLI first and give it the path to the PHP code you want to run.

For example, you could also use the path:

/usr/local/bin/php /path/to/PHP_SOURCE_FILE

In this example, you would change "/path/to/" to the actual path to the PHP file, and change PHP_SOURCE_FILE to the file name of the PHP code.

The default /usr/local/bin/php is now PHP 7.1. But there are other version such as PHP 5.6 at /usr/local/bin/php56 or PHP 7.3 at /usr/local/bin/php73.

Users can run the following command to see all of the available PHP versions:

ls /usr/local/binphp*
Call a Program

A third option is to call a program like wget on the URL of the PHP script to have it run through PHP.

/usr/local/bin/wget -q -O - http://yourdomain.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1

The -q stands for "quiet;" it turns off the wget output.

-O - Tells wget to write to the standard output

>/dev/null redirects the standard output to /dev/null

2>&1 redirects the standard error to standard output, which was redirected to /dev/null

Cron Policy

The use of cron on our systems is subject to our published policy of cron usage. Please review this policy before configuring any cron jobs.

Updated on August 29, 2022

Was this article helpful?

Related Articles

Need Support?
Can't find the answer you're looking for?
Contact Support