How do I create a cron job?


If your hosting account is a shared hosting account: 

  • Login to our Dashboard: http://dashboard.brownrice.com
  • Click Site Hosting & Email -> Manage Web Site -> Cron Jobs (button)
  • Follow instructions!  And that's it.

If you have any issues, questions, or would like us to do this for you just contact us and we'll be happy to take care of you.

If your account is a VPS: 

  • Login to our Dashboard: http://dashboard.brownrice.com
  • Click VPSs -> VPS name Manage -> Cron Jobs (button)
  • Follow instructions!  And that's it.

If you have any issues, questions, or would like us to do this for you just contact us and we'll be happy to take care of you. 

If your account is a VPS account and you prefer to work via the command line: 

  • SSH to your VPS (Don't know how?  Follow these instructions first.)
  • Once logged into your VPS via the command line (any user account can do this, not just root) type:

crontab -e

This will open a blank file where you will enter your cron commands.  Type the letter "A" on your keyboard to enter edit mode so that you can enter your command. 

The example comand below would open a web page every minute of every day.

* * * * * curl http://mywebpage.com/script.php

Here's an explanation:

Cron formatting

 

This example would run a php script from the command line at 5:15pm every day (17:15):

15 17 * * * php /home/domains/mywebpage.com/docs/script.php

This will run a script every fifteen minutes. You can place multiple commands in a cronjob, just separate them with a semicolon. Here, we cd (change directory) to the location of the php script THEN execute it. If you're having problems getting a cron job to work, try doing it this way.

0,15,30,45 * * * * cd /home/domains/mywebpage.com/docs/; php script.php

This will run a script every fifteen seconds! - It is actually 4 separate jobs that start running at the same time (once a minute, every minute), but each one sleeps progressively longer than the last before executing the next command in the job.

* * * * * cd /home/domains/mywebpage.com/docs/; php script.php
* * * * * sleep 15; cd /home/domains/mywebpage.com/docs/; php script.php
* * * * * sleep 30; cd /home/domains/mywebpage.com/docs/; php script.php
* * * * * sleep 45; cd /home/domains/mywebpage.com/docs/; php script.php

Need to periodically download a file from another site? Just use wget! No need to script it in PHP.

* * * * * cd /home/domains/mywebpage.com/docs/downloads; wget http://example.com/somefile.zip

And if you want to have an email sent to you each time your cron runs enter this command:

MAILTO=username@youremailaddress.com

When you are done editing the file you'll need to leave "edit" mode by pressing your ESC key, then you'll type ":wq" to save the file and quit the cron editor.

That's it!  Your cron command(s) will run as often as you asked them to from this point forward.

A few notes on cron jobs:

  • You can enter as many cron commands as you like within one file.  Just keep each command on a seperate line.
  • If you ever need to modify your cron command just follow this tuturial again and you'll see the commands in place (as long as you login as the same user)
  • We recommand that you test your cron commands from the command line before entering them into the cron file to make sure they work.  i.e.  SSH into the machine, then type the command at the command line like this example "php /home/domains/mywebpage.com/docs/script.php" then press enter.  If no errors then enter into your cron job!  If there are errors then fix'em and try it again.