Wednesday, June 21, 2006

Cron job - quick & dirty

Here's how you set up a cron job on a linux machine:

  • Write the shell script you want to execute wherever you want. Say, /tmp/

  • Try executing this script manually from the console. Make sure it does what you need.

  • >sudo vi /etc/crontab
    Here I'm assuming that you have root access using sudo and you know that you ought to be careful with sudo etc.

  • Now in this file, you will see something like this:

    47 1 * * * root run-parts /etc/cron.daily

    This means : Run all scripts under the directory /etc/cron.daily every day at 01:47 (AM). See what your crontab file is set up to do. Here is the generic description of the format:

    00,30 12,13,14 1 4 3 <user> run-parts <dir>
    00,30 This is where you specify the minutes (0-59) We have
    chosen both :00 and :30 (right on the hour and
    half hour)

    12,13,14 These are the hours, it is in military time so 0-23
    (this example equals to 12pm, 1pm, and 2pm)

    1 The day of the month (1-31) This is of course the first
    day of the month.

    4 This is the month (1-12) April in this case

    3 This is the day of the week (0-6 with 0 being Sunday)
    Wednesday is being used in example.
    <user> Run the cron job as this user
    <dir> Run the cron job parts (scripts) from this dir

  • Based on the contents of your crontab file, copy your script in the right place. In my case it would be: >sudo cp /tmp/ /etc/cron.daily/

  • Make sure the script is executable.
    >sudo chmod +x /etc/cron.daily/

  • That's it! Now to test your cron job, look at the system date as:

    Wed Jun 21 03:34:00 PDT 2006

    Assuming you got the above output for the system date, you can schedule the cron job (cron.daily) to run at the next minute as:
    34 3 * * * root run-parts /etc/cron.daily

That's of course, the quick & dirty way to get you going. If you need more information, just do man crontab or google it!

Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?