World Class Web Hosting
+1.877 724-7638

my pair account webmail

Web hosting services

 

  • pairNIC - domain name registration (as low as $13/year)
  • pairSIM - one-click software installation
  • pairCDN - content delivery network
  • pair reseller program - reselling our Web site hosting services
  • pair Networks does not offer "Business Associate" services under HIPAA

about pair Networks

 

Contact Us

pairNIC Support

blog.pair.com

Call Us

  • +1.877 724-7638
  • +1.412 381-7247

Fax

  • +1.412 381-9997

E-mail Us

Web contact forms

Mailing Address

  • pair Networks, Inc.
    Suite 210
    2403 Sidney Street
    Pittsburgh, PA 15203-2168

support - knowledge base

 

Authoring/Development - Configuring Cron

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

Advanced users are welcome to use cron via the Unix shell, or you can use the Account Control Center to easily setup and maintain a schedule of programs to be run.

What is cron?

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

Controlling cron via the Account Control Center

The Account Control Center allows easy access to cron. We recommend that all customers use this interface to use cron, although advanced users are welcome to use cron manually, as explained in the next section.

As usual, to begin configuring your account via the Account Control Center, you must first log in at:

https://acc.pair.com/

Once logged in, select the Advanced menu, and then select the "Crontab Manager" from the list of options.

Each entry in your crontab is known as a "cron job". To add your first job, click on the "Add New Cron Job" button at the bottom of the page. The first screen presented will allow you to select whether the job should run daily, weekly, or monthly. Make your selection and click "Proceed".

Depending on your selection, the first part of the next step will vary:

  • Monthly Jobs - the first part of the scheduling form will ask you to select the day of the month on which the job should run.
  • Weekly Jobs - the first part of the scheduling form will ask you to select the day of the week on which the job will run.
  • Daily Jobs - for daily jobs no such scheduling is required, since the job will run every day.

After selecting your interval, the next section of the form allows you to select the time of day the job will run. Here you are given two choices. Normally, cron jobs are scheduled to run at a specific time or times of the day. This may be specified on the form; please note that the time is specified for the Eastern time zone of the United States (where our main datacenter is located).

It's important to understand that our servers are set to the Eastern time zone as 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 Daylight Saving Time hour "gap" (between 2 A.M. and 3 A.M.), 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.

In addition, the Crontab Manager offers a special feature - "preferred time scheduling". This feature allows your job to be scheduled by the system at a time unique to your account, so that other users are less likely to have jobs running simultaneously. This improves the performance of all jobs, as well as Web server performance overall. We highly encourage the use of this option whenever possible, especially if you are scheduling an "overnight" process or one that processes your Web logs, as many other users may be doing the same.

The remainder of the form allows you to specify the job you would like to run. Be sure to specify the full path to each program used in the job. For instance, instead of just "cat" you should use "/bin/cat" to use that Unix command. To run a script in your home directory, use "$HOME/script.pl" for example. When you run a custom script, it must be marked executable (chmod 700, or use the permissions setting in the File Manager).

When you are finished, click the "Add Cron Job" button to complete the process. 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 main Crontab Manager screen. By clicking on the job, you'll be taken to another form where you are given the ability to edit the job, or delete it entirely.

One final note if you wish to run PHP scripts via a cron job and run the code via the command line. You can accomplish this task by either changing the starting line of the code to be a full path to a CGI copy of PHP or by calling the PHP CLI first, giving it the path to the PHP code that you wish to run. A third option is to call a program like wget on the URL of the PHP script to have it run through PHP.

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

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

The file could then be called with the full path to the file. 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.

Advanced cron usage

To use cron manually, you must first set up a file of cron entries. This file can actually be placed anywhere in your account you like, and can have any filename you want ("crontab.txt" would be a good choice). It should contain one entry for each job you are scheduling.

A cron entry looks similar to the following:

30 3 * * * /usr/bin/nice /usr/home/username/script.pl

In this case, the script "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 here if you like, using full paths to programs. For instance, you could have:

30 3 * * * /usr/bin/nice /bin/rm -f /usr/home/username/temp/*

to remove all files from some temporary directory you might have, for example.

The "30 3 * * *" requires a bit more explaining. 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 Eastern).
  • 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.).
An asterisk (*) in place of any of the values matches all possibilities (for instance, in the given example the script runs every day because the *'s 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.)

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

crontab crontab.txt

using the name you gave the file. (Note that you must be in the same directory as the cron file at the time.) After this, your cron should be activated. If the programs running via cron generate errors, the cron daemon will e-mail them to your account.

If at any time 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 back up your site's files. To use these commands, copy the command and paste it into the "Command To Run" field in the ACC's cron interface.

To back up your home directory, skipping 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

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

To back up your public_ftp directory (if you have one):

/usr/bin/tar czf /usr/bin/tar czf /usr/home/username/backup/ftp.`/bin/date +\%Y\%m\%d`.tar.gz /usr/public_ftp/username

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

In all of the commands above, please replace "username" with your pair Networks username.

To back up a particular database, enter the following command in the "Command To Run" field:

/usr/local/bin/mysqldump -hDBXX.PAIR.COM -uDB_USERNAME -pDB_PASSWORD USERNAME_DATABASENAME > /usr/home/USERNAME/backup/DATABASENAME.`/bin/date +\%Y\%m\%d`

For the above command, you'll need to replace the following items according to the table below:

DBXX.PAIR.COM - The hostname of the database server the database resides on
DB_USERNAME - The MySQL username for the database in question
DB_PASSWORD - The MySQL password for the username above
USERNAME_DATABASENAME - The full name of the database
USERNAME - Your pair Networks username

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, either manually or through the Account Control Center.