HOWTO: Set up a CRON Job

Posted in Technology
Mon, Mar 20 - 7:54 am EDT | 8 years ago by
Comments: 2
Share This Post:
  • Facebook
  • StumbleUpon
  • Tumblr
  • Reddit
  • Twitter

My Cron JobsCRON Jobs is the mechanism by which you can tell your GNU/Linux box to perform specified tasks at specified times. Cron (a typical Linux short-cut for the term ‘chronograph’ or perhaps ‘chronology’) is generally used on servers to do things like log rotation and other mundane maintance, but there’s no reason why Cron can’t be used on home GNU/Linux boxes as well.

A great candidate for a Cron job is telling Bashpodder to go fetch your podcasts at a certain time each day. For the sake of simplicity, that’s going to be my example for this entry. Cron can do darn near anything you’d like at any time of the day but we’re just going to use a simple, daily task for this example.

Open up a terminal window and type:

crontab -e

This will open up your own personal crontab. If you’ve never used Cron before, you’re likely staring at a black editor screen.

A crontab is the ‘schedule’ of tasks that cron is to perform. It has two basic parts to it: the schedule and the command to run. The schedule part is broken down into sub-parts, but they’re pretty easy to understand. The first line of my cron file looks like this:

04 * * * * ~/bashpodder/bashpodder.sh

The first part is the ’04 * * * *’ bit and the second part is the command, ‘~/bashpodder/bashpodder.sh’. This tells Cron to run the bashpodder.sh script at 4 minutes past each hour. Now, of course my machine has to be on for Cron to perform the task, but that’s about all there is to it. I suppose that running bashpodder hourly is a little demanding on my podcast producers, so let’s mess around with the astericks fields.

The astericks in the line above each represent a specific time interval (minute, hour, day, month, day of week). You can put values in those spaces, but the astericks means ‘every one’. So, the line above means run bashpodder.sh at 4 minutes past the hour every hour of every day of every month.

04 01 * * * ~/bashpodder/bashpodder.sh

Would only run the command once every 24 hours – at 4 minutes after 1 in the morning.

04 01 02 * * ~/bashpodder/bashpodder.sh

Would only run the command once a month at 4 minutes after 1 in the morning on the 2nd of each month.

04 01 02 05 * ~/bashpodder/bashpodder.sh

Will only run the command once a year at 4 minutes after 1 in the morning on the 2nd of May.

The last time interval indicator is the day of the week using the first three letters of the day.

04 * * * sun ~/bashpodder/bashpodder.sh

Will run the command every four minutes of every hour on every Sunday regardless of the month or day of the month it falls on.

There are some special intervals you can use. For example, if I wanted bashpodder to run every five minutes, I could use:

*/5 * * * * ~/bashpodder/bashpodder.sh

The only two caveats is that your command must be indicated using the fully qualified path (/home/jon/folder/command.sh), although I’ve found that the ~ (home folder) token works. The reason for fully qulalifying the path is that Cron doesn’t start from the same place that you do when you’re logged into your account.

Lastly, I can just run bashpodder.sh because the Bash shell knows how to run its own scripts. However, if I wanted to run a PHP script, I’d have to tell Cron where PHP is as well as what PHP script to run.

04 * * * * /usr/bin/php ~/jon/myphpscript.php

Should do the trick.

This is a tricky topic. Corrections or comment, anyone?

Share This Post:
  • Facebook
  • StumbleUpon
  • Tumblr
  • Reddit
  • Twitter
  • http://franktank.com/blog/linux/install-ubuntu-dual-boot-with-vista-installed-on-your-laptop/ Frank

    Hey Jon,

    Great clear tutorial thanks. Also, I’m pretty sure you can just place #!/usr/bin/php at the very start of your script to avoid entering that data in the cron tab.

  • D.Ksyte

    Anyone involved with cron job scheduling might find this resource useful.
    Cron Sandbox at HxPI is an interactive webpage where you can play with crontab command strings.
    Enter your ‘m h D M Dw’ parameters and immediately see all the times cron would run your job in the coming days/weeks.
    Newcomers to cron job scheduling get a safe place to learn crontab commands and try examples from tutorials.
    System Administrators get a forward schedule to help manage system loading.
    http://www.hxpi.com/cron_sandbox.php