How Do I Schedule A Task (Cron Job)?


This article contains general information about scheduling tasks on the Crontab. On the Plesk servers, the crontab is called Scheduling Tasks and can be accessed from your hosting control panel. 

  1. Once logged in, select the subscription you wish to work with. If you only have one subscription, this step is skipped. 
  2. Click on the Websites & Domains tab.
  3. Click on Show Advanced Operations (should be in grey just below the last icon in the first column).
  4. Click Schedule Tasks.
  5. Click on the system user hyperlink (should be the same as your main ftp user).
  6. Click Settings and define the email address that the system should use in the event of an error.  Note: If you don't use a proper email address your script may be disabled without warning!
  7. Click Add New Schedule Task.
  8. Now define when you want your script to be triggered. If you want your script to be triggered at 4:27am and 4:57am every Sunday, put 27,57 in the minute column, put a 4 in the hour column, and fill in everything else with a *, except for day of the week, for which you would select Sunday.
  9. Path: The path will be /usr/bin/php -q e.g. /usr/bin/php -q /var/www/vhosts/yourdomain/httpdocs/yourcronfile.php. The -q suppresses HTTP header output. As long as your script itself does not send anything to stdout, -q will prevent cron from sending you an email every time the script runs. For example, print and echo send to stdout. Avoid using these functions if you want to prevent cron from sending you email.

Questions

Q) How come my script works fine when executed via the web but not using Schedule Tasks/Cron, what's wrong?

A) Cront tasks are executed on the command line so standard apache variables like $_SERVER['DOCUMENT_ROOT'] won't have values defined.

you could populate the $_SERVER['DOCUMENT_ROOT'] on your own

$_SERVER['DOCUMENT_ROOT']= dirname(__FILE__);

if the cron file is in document root

$_SERVER['DOCUMENT_ROOT']= dirname(dirname(__FILE__));

if the cron file is one directory above the document root

More information can be found here.