feedback

Scheduling programs using crontab

Calendar

(Picture from meds.queensu.ca)

On any Linux there are two commands for scheduling at and cron. Programs scheduled by command at will be executed once at a defined time. With crontab it is possible to define a command that will be executed periodicly.
To make sure you can use crontab check if there is /etc/cron.allow file and if it exsists check for your username there. If you use a Debian based Linux you don’t need to worry, you’re may use it by default.
Crontab saves configuration in a simple text file and by entering crontab -e you can edit it in terminal. The syntax for is simple:

time command

Time is construced like this
m h dom mon dow

  • m is for minute
  • h is hour
  • dom is day of month
  • mon is month
  • dof is day of week

So a simple example would be:

0 0 2 2 1 dd if=/dev/sdb of=./backup

This will backup a device at /dev/sdb at midnight 2nd February and this command will be executed once, to make it run repetitionly, you will need to put asterisk (*) in the place needed. For example if you want to run this command at midnight every day, put asterisk(*) in every place except m and h:

0 0 * * * dd if=/dev/sdb of=./backup

For weekly repetition (every monday):

0 0 * * 0 dd if=/dev/sdb of=./backup

You can also define a range using a hyphen (-) to define a range and a comma for multiple values:

0 12 * * 0-4 mpeg321 ./dont_worry_be_happy.mp3
0 10,16 * * * mailprogram

The first command plays “Don’t worry be happy” every day from monday to friday at 12 and second one checks your mail twice a day.
And for the end the most interesting, to schedule GUI apps with crontab use: export DISPLAY=:0 && app-name.

Related Posts

No related posts.

Digg icon Delicous icon Stumbleupon icon Reddit icon Twitter icon Facebook icon Technorati icon

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

2 Comments to Scheduling programs using crontab

  1. morgan's Gravatar morgan
    1. February 2010. at 12:17 | Permalink

    I always cheat and use the cron GUI – Kcron

    (kde4 it is is system settings / task scheduler), I find it far easier as you construct the tasks in Enlgish.., you can then look to what the correct cron syntax to use )

    - As usual gnome's equivalent lacks the features a KDE app.

  2. 3. February 2010. at 10:49 | Permalink

    I also like to use gui apps more, there is no reason to make my life harder, but crontab is very easy.

Leave a Reply

You can use some HTML tags

Subscribe by RSS or mail

RSS icon Mail icon
Benefits of subscribing

Tweet, tweet

Twitter bird

"Entrepreneur Heroes Trading Card Series http://bit.ly/5iovX9"

Follow Bojan on Twitter!!

Random Quote

quote openJust remember: you’re not a ‘dummy,’ no matter what those computer books claim. The real dummies are the people who–though technically expert–couldn’t design hardware and software that’s usable by normal consumers if their lives depended upon it.quote end
by Walter Mossberg

Tag cloud