Using Cron


  • Applies to: DV
    • Difficulty: Easy
    • Time Needed: 10
    • Tools Required: Plesk administrator access
  • Applies to: DV 4.0
    • Difficulty: Easy
    • Time Needed: 10
    • Tools Required: Plesk administrator access
  • Applies to: Grid
    • Difficulty: Easy
    • Time Needed: 10
    • Tools Required: Account Center access

Overview

Cron is a server utility that allows you to set up scheduled tasks. You might want to set up a cron job to do any of the following.

  • Check disk space.
  • Run a maintenance script.
  • Delete files periodically.

Limitations

Please be aware of the following limits that are enforced for all cron jobs on the Grid.

  • 5 minutes of CPU time
  • 15 minutes of actual time
  • 200 simultaneous file descriptors
  • 100MB of RAM
  • 5 simultaneous processes

READ ME FIRST

(mt) Media Temple supports the basic cron functionality for your Grid. Troubleshooting specific cron jobs is outside the scope of our support. Please take a moment to review the Statement of Support.

Instructions

  1. Sign in to the Account Center.
  2. Select your domain from the DOMAINS tab.
    ac_mainmenu_domains
  3. Click Cron Jobs from the FILE MANAGEMENT panel.
    acGRIDmainmenu_filemanagement_cron
  4. Click Add Cron to continue.
    acGRIDmainmenu_filemanagement_cron_add
  5. Enter an email address for notification purposes. (optional)

    acGRIDmainmenu_filemanagement_cron_add_email
  6. Fill out the Command or script to execute field. (required)

    acGRIDmainmenu_filemanagement_cron_add_email

    • Example of a disk usage command or script to execute: This is a simple one line command.
      du -sh /home/00000/

      Replace 00000 with your site number.

      If your cron is more than one line, it is highly recommended that you save it as a script (see the next example for details). If you wish to run a custom script, make certain you've uploaded the script to a directory on your server. Then provide the complete path to your script. This is an example of a full path to a script.

    • Example of running a Shell or Perl script
      /home/
    • Example of running a PHP script via command-line
      php /home/00000/data/script-name.php
    • Example of running PHP5 via the command-line
      php5 /home/00000/data/script-name.php
    • Example of running a PHP script via curl. The command-line curl client allows you to fetch a page from a webserver and the output is sent to you via email (if the email field above is completed).
      curl http://example.com/script-name.php

      Another option for curl includes adding the silent option so that you will not receive emails unless errors are reported.

      curl --silent http://example.com/script-name.php
  7. Choose the time and frequency with which you would like to run your script. You can use some of the predefined values like Every hour or Every 5 Minutes. If you use the Specify option, the list of numbers allows you specify what minute of the hour you want the cron job to run. For example, if you select '3', then the cron job will run at 10:03, 11:03, 12:03, etc. You can select multiple numbers, but they must be at least five minutes apart.

    NOTE

    The maximum frequency that your script can be run is in five minute intervals. The cron will use the PST time zone.

    acGRIDmainmenu_filemanagement_add_sched

  8. Review the values you have entered into each field and click SAVE. If you selected to have the output emailed to you, it should arrive shortly after the command or script has finished. The screen will refresh and your list of cron jobs will be listed.

Notes

  • If you are running a shell script instead of a built-in command, you must make certain that your script is executable and has the correct ownership. The command following will make your script executable.
    chmod +x /path/to/script
  • In any field, you can select multiple settings by holding the Ctrl-key (PC) or command-key (OS X) down while clicking on your selection to highlight multiple entries.
  • Running a php script via cron will not load a site's php.ini. This can be forced with the following flag.
    -c /home/00000/etc/php.ini

    This will allow users to, e.g., retrieve remote data (by turning on allow_url_fopen) using a php script outside of the web root.

READ ME FIRST

(mt) Media Temple supports the basic cron functionality for your standard configuration DV server. Troubleshooting specific cron jobs is outside the scope of our support. Please take a moment to review the Statement of Support.

Instructions

Parallels provides a thorough walkthrough for accessing the cron feature through the Plesk control panel. Note that you can set up either a server-wide cron job or a domain-specific cron job.

Examples and Tips

The scheduling format is the same in Plesk as it is for manual cron jobs. There are five scheduling fields followed by a command string.

  • Minute: Choose 0-59
  • Hour: Choose 0-23 (24 hour clock)
  • Day of the Month: Choose 1-31
  • Month: Choose 1-12
  • Day of the Week: Choose 0-7 (0 and 7 are both Sunday)
  • Command: This is the command that will be entered in the crontab. For a quick test, try running the command from SSH first. Make sure you are logged into SSH as the appropriate user (the one you clicked on to set the cron job in the first place).

More Scheduling Options

  • List: 0,15,30,45 in the Minute field will run every quarter hour.
  • Range: 4-6 in the Day of the Week field will run on Thursday, Friday, and Saturday.
  • Every: * means "every." If you put * in each field, the job will run every minute, all the time.
  • Divide: / allows you to specify a time that is divisible by a certain number. For example, */2 in the Month field will run on even months only.
  • Results will be emailed to the user who owns that job by default, at the host name of the server. Example: If you set up a job under your domain, with the user domainuser, the email will go to domainuser@example.com. You can designate a different email address if desired. To do this, follow Steps 1-3 of the Plesk walkthrough (linked above), then click on Preferences. Here, you can set a new email address.

Examples

TIP

Remember that the first five characters are scheduling characters. You do not need to copy and paste these into the Plesk control panel - just the command part. On the other hand, if you are editing the crontab manually, you should include the entire command.

  • Check disk usage every hour on the hour and email the results to username@example.com.
        0 * * * * df -h | mail -s 'Email subject' username@example.com
      
  • Run a perl script at 2:15 PM every Monday. Standard output will not be emailed, but errors will still go to the standard email address for this cron user.
       15 14 * * 1 perl /full/path/of/script.pl > /dev/null
      
  • Delete a file on the 15th and 30th of each month. No output (standard or error) will be emailed.
      * * 15,30 * * rm -f /full/path/to/file &> /dev/null
    

Edit Cron Manually

CAUTION

(mt) Media Temple does not support manually editing your crontab outside of the Plesk control panel.

You can do even more with cron by using the manual crontab editor. To open the crontab for editing, complete the following.

  1. Log into your server with a root or sudo user via SSH.
  2. Check for existing jobs (not strictly necessary, but a good idea to prevent conflicts).
               crontab -l
             
  3. Open the crontab for editing.
               crontab -e
             
  4. You can edit the file with vi. Make your desired changes, then save.

    TIP

    vi tip: Press i to enter insert mode so you can type and copy/paste. Press Esc to exit insert mode when you are done modifying the file. Type :wq to save and quit.

Your new cron job should now run as specified.

You can use scheduling shortcuts when you edit your crontab manually. The shortcut comes before the command in place of the five numbers. See the following table for possible values.

stringmeaning
@reboot Run once, at startup.
@yearly Run once a year, "0 0 1 1 *".
@annually (same as @yearly)
@monthly Run once a month, "0 0 1 * *".
@weekly Run once a week, "0 0 * * 0".
@daily Run once a day, "0 0 * * *".
@midnight (same as @daily)
@hourly Run once an hour, "0 * * * *".

Plesk Instructions

    1. When using the Service Provider View (you have a sidebar)
      1. Under Hosting Services, select Domains.
      2. Choose your domain. Click Open in Control Panel. The Power User view will display. cron_domains
        For your domain, click
        Open in Control Panel.
        cron_domains
    2. When in the Power User view (the Control Panel)
      1. Select the Websites & Domains tab. Click Scheduled Tasks.
        cron_sched_tasks
        Select
        Scheduled Tasks.
      2. Select the user.
        cron_user

      3. Click Schedule New Task.
        cron_schedule_task 
      4. Specify when to run the command by assigning time/day values. Complete the Command.
        • Switched on
        • Minute
        • Hour
        • Day of the month
        • Day of the week
        • Command
      5. Assign the values for your cronjob command.
        cron_new_task_scheduling
    1. Click OK.

Additional Reading

There are many cron resources online.