feedback

Scheduling programs using crontab

Calendar

(Picture from meds.queensu.ca)

Every Linux has two commands for scheduling: at and cron. Programs scheduled by command at will be executed once at a defined time. With crontab it’s possible to define a command that will be executed periodically.
To make sure you can use crontab, check if there is /etc/cron.allow file and if it exists check for your username there. If you use a Debian based Linux you don’t need to worry, you 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 is simple:

time command

Time has this syntax:
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

Simple example:

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

This will backup a device /dev/sdb at midnight 2nd February, this command will be executed once, to make it run repetitionly, you will need to put asterisk (*) in the place needed. 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 (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 && command.

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

3 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.

  3. 2. May 2010. at 13:02 | Permalink

    great post as usual!

Leave a Reply

You can use some HTML tags

Random Quote

quote openThe most important thing in a programming language is the name. A language will not succeed without a good name. I have recently invented a very good name and now I am looking for a suitable language.quote end
by Donald E. Knuth

Tag cloud