« Fieldrunners: best iPhone game to date? | Main | Wow...just wow. »
Thursday
Oct162008

How to setup a linux cronjob in 3 steps

Since web apps are executed only after an HTTP request, you may have situations where scripts need to run in the background on a scheduled basis. On Linux, to do this you'll need to get to know the crontab. The way it works is kind of like an airport. The service is always running, however planes only depart at their scheduled times. The crontab is kinda like that, a daemon that runs constantly in the background and checks once a minute to see if any jobs need to be executed.

Let's say you have a script located at http://www.acme.com/myscript.php that needs to run the first of every month at 9am. Here's how you'd set it up:

1. Look for a file in your /etc folder called crontab.
2. You need to install a new crontab job, to do this type crontab -e.
3. Now the tricky part, the syntax:

and here's how that translates in our example:

0 9 1 * * curl http://www.acme.com/myscript.php

Lets break it down. 0 is for the minute, 9 is the hour - so 9 hours & 0 minutes which is 9am. Day of the month, 1 - easy. We want to do this for every month & week, which is accomplished using the wildcard or * character. Next comes what we want to execute. It's not enough to just type the URL of the script, you'll need to use the CURL system function to make the HTTP request for you.

Here is some more information on CURL & Cron jobs for your reading pleasure, hope this tutorial helps!

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>