If you like to run some command at certain intervals, cron
is probably the best choise for you. If you'd like to time a command to be run at some time, like an alarm clock in the morning, you could use at
. A daemon, on the other hand, is considered a program that sits in the background waiting for something and acting as the "something" happens, for example -- so it's running all the time
, not just at certain intervals. If you wish to execute a perl script, use cron:
will open the crontab file (file which includes the commands to be run along with the times; one per line) for the current user. You could also add an -u user
option to edit user
The file is edited as follows: each line contains one command to be executed at the given time. The line goes like follows:
* * * * * /path/to/command --possible-options
the stars you see in the front tell when the command is to be run. A star like above means "all the time" or "always" (so don't put all stars). I don't necessarily remember which star is which, but it reads in the man page of crontab:
If I remember correctly, the first one means minutes (0-59), second means hours (0-23), third means the day (1-31), fourth the month (1-12) and the last one the day of the week. They might also be in a different order, so read the man page to make sure.
Basically you just put them there, a number, space, number, space etc. and after the weekday a space and the command, just as you would run it from the console. It's a good habit to write full paths (i.e. /usr/bin/date
instead of date
). And like I said, if you want to omit some time ("run every day of the month"), use a star. Like
10 12 * * * /usr/bin/date > ~/date.txt
which should run /usr/bin/date
and put the output into a file called date.txt
under your home directory, and run the command 10 minutes past 12 every day of the month, every month and every weekday (weekday is useful, if you only want to run the command at Mondays, and only if Monday is some certain day like 16.4. or only at Tuesdays in March).
You now notice that you can't give smaller times than "each one minute". How to run a command every 10 seconds? Easy - write a shell script that runs the wanted command, waits for 10 seconds, runs it again etc. until it has done so for a full minute. Make cron run that script every minute and -- ta-daa, it's there
I hope this cleared things a bit. I know the format of the crontab file is confusing, but if you read the manpage I suggested, it has got some examples at the bottom which should clarify the format to you.
If the crontab file is opened in vi
editor (you don't edit the file directly, crontab
command does this for you), here's a quick guide on how to use it:
- press INSERT
(big i) to enter the INSERT MODE - this is where you type text (otherwise you just wander around the file and do other stuff)
- once you've written/edited the file, press ESC
to get back to COMMAND MODE - this is where you give commands to Vi.
- once you're in COMMAND MODE, type :w
) and ENTER to save (write) the file, then :q
and ENTER to quit -- or just write :wq
to save and
quit at the same time.
Another way around is to use your favourite editor by setting the console editor variable to point to your favourite editor before running crontab
EDIT: in most cases it's a good habit to press ENTER to add a linefeed at the end of the last line, so that the last line of the file becomes an empty line; it's not always needed, but in some cases parsing a configuration file may fail at the last line if it's not empty; some programs tend to expect the file to end at an empty line. I'm not sure if crontab wants this, but generally it doesn't harm you to insert one blank line at the end of the file (and not leave a complete written line to be the last one).