How do I create a cron job?


A cron job (run by the cron daemon, or cron service) is a request for the server to run a particular command and/or program via the command line and set points during the hour, day, week, month or year.


The name is derived from Greek chronos (χρόνος), meaning time.


At the moment, there are two ways you can add, edit or delete cron jobs on our servers. The first is the easiest and that's using the Plesk interface. The second is more complex, using the crontab program, but if you're used to using the SSH console and have knowledge of VIM, it's available.


However, if you're not familiar with the way cronjobs are configured, please read the following section which will explain the syntax and how it's works.


What does a cron line and how does it work?


A cronjob line if a very simple line with six parts. The first five set the day/time the job is/are run, while the last part is the command to be run. For example:


0 * * * * php /var/www/vhosts/example.com/httpdocs/cron.php

will run the php command-line interpreter, processing the file cron.php (on the website for example.com) at the start of every hour.


The date/time fields have the following meaning:


# +------------ Minute (0 - 59)
# | +---------- Hour (0 - 23)
# | | +-------- Day of the Month (1 - 31)
# | | | +------ Month (1 - 12)
# | | | | +---- Day of the Week (0 - 7) (Sunday is 0 or 7)
# | | | | |
# * * * * * command

Therefore, the option 0 * * * * (from the above example) means 'on the zeroth minute of any hour on any day of the month, in any month, on any day in the week, run this command'.


Although can you add a single value, the power of cron comes in it's ability to allow a number of different syntax options for specifying different values. The following examples will be based on the minutes field, but they can be used in any of the five fields to specific month, day, etc.:


0          - Run it when the minute is zero
* - Run it on any value (i.e. every minute)
0,15,30,45 - Run it when the minute is 0, 15, 30 or 45 (i.e. every 15 min)
*/15 - Same as above - run it when the minute is divisible by 15
10-15 - Run it every minute between 10 and 15 minutes
30-59/5 - Run it every fifth minute of 30 (i.e. 30, 35, 40, ...)

However, taking the last example, the divisor is not against the minute of the hour, but against the minute within the period selected, therefore while 30-59/5 is when the minute can be divided by 5 between 30 and 59, the option 3-58/5 will not enact on 5, 10, etc., but on 3, 8, 13, etc.


Also, where as the first four fields must match to run the command (i.e. when the minute, hour, day of the month and month match), if the fifth field (day of the week) is also specified, the command will run when either the day of the month OR day of the week match.


To finish off, here are a number of complete examples:


*/15    *     *  *  *   - Run every 15 minutes
3-58/5 * * * * - Run every 5 minutes on the 3rd and 8th minute
0 */2 * * * - Run every 2 hours, on the hour
25 8,17 * * * - Run at 08:25 and 17:25
0 6 * * 1 - Run at 06:00 every Monday
30 7 * 1 * - Run at 07:30 on the first day of the month
0 0 1 * 5 - Run at midnight on the first of the month OR a Friday
*/30 9-17 * * 1-5 - Run every 30 minutes between 9 to 5, Monday to Friday

Editing your crontab with Plesk


To add a cron job in Plesk:



  • Log onto Plesk using your username and password.

  • From your list of domains, click on the domain you would like to add the cronjob to.

  • Click on Crontab.

  • If you have multiple users on this domain (i.e. you have setup different FTP accounts for sub-domains or web users), from the drop-down select the username you want to create the cronjob for, and then click Set.

  • Click on Add New Task.

  • For each of the first five fields, enter the value as it would be for the cron line (i.e. the date/time fields). All fields are required, so if you don't have a specific value in that field, enter *.

  • Finally, enter the full command into the Command field.

  • Click OK to save or Cancel to return.

  • If you return back to the page with any errors, please correct the errors and click OK again.


By default, the sever will send any error messages or output from the cronjobs to username@server, e.g. mysite33@gamma.jabservers.co.uk, which will always come to us. If you want to see the output from your cronjob, or notices if it doesn't run (for whatever reason), you'll need to change the address the e-mail is set to.

To do this, goto the crontab page and select the user running the crontab, click on Set, and then change Send Crontab messages to address to the e-mail address you want them sent to. Click Set again to commit the change.


Editing your Crontab via SSH


The main limitation with this option is that you can only edit the crontab for the main domain, as you can only log on via SSH using the user for the master FTP account. If you need to change the crontab for a web user or sub-domain, you will have to use the Plesk interface.


To add a cron job via SSH:


Although we will take you through the steps of using VIM on the server, knowledge of VIM is important for using the crontab program - if you do not know how to use VIM, please see how to add a cronjob using Plesk above.



  • Log onto SSH using your chosen program and the username/password for the domain.

  • Run the command 'crontab -e' to edit the crontab.

  • Move to the end of the file, press i to enter Insertion mode, create a new line and then enter the cron line.

  • Press Esc and then enter :wq to write (save) the changes and quit the program.

  • If there is an error in the line, you will be notified and asked if you want to re-edit your changes.

  • If there is no error, you will see the message 'crontab: installing new crontab'.


Again, like with Plesk, any errors, notices or output from the cronjob will go to username@server (e.g. mysite33@gamma.jabservers.co.uk), which, as we manage server, or gamma.jabservers.co.uk in the example, the e-mails will come to us.

To change this and tell the cron daemon to send the e-mails to you, add the line 'MAILTO=example@example.com' to the top of your crontab.

  • 155 Usuários acharam útil
Esta resposta lhe foi útil?

Artigos Relacionados

How do I set-up custom error pages in Plesk?

There are two different ways to display error messages using Plesk on Linux. The first is...

How do I set-up custom error pages using .htaccess?

There are two different ways to display error messages using Plesk on Linux. The first is through...

What is Hotlinking and how do I prevent it?

If you notice a surge in the traffic coming to your site, it may not be because you are receiving...

What is an injection-based Spam attack?

Many of the older techniques for sending Spam out via remote servers either involves searching...

How do I create an SSH tunnel?

The SSH protocol is a useful tool available on all our servers which allows our customers to...