cron

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The syntax of the crontab file is downright odd, but it's been this way in
just about every Unix system or Linux system I've encountered.

The first entry is the minute or minutes on which you want your cron job
to run.  For every 15 minutes, you would use "0,15,30,45, in the first
field.  The second field is the hour.  It is expressed in 24-hour notation
from 0 to 23.  The third field is the day of the month you want to run the
cron job.  The fourth field is the month or months of the year to run the
cron job.  The fifth field is the day of the week from 0 for Sunday to 6
for Saturday.  All of these fields can take a single value, a
comma-separated list of values, a range, or an asterisk which means any.
For example, if you wanted the time announced every 15 minutes, and you
had a command called "saytime," you would put something like this in your
crontab file.

0,15,30,45 * * * * /usr/local/bin/saytime >/dev/null 2>&1

It is generally a good idea to include the full path to commands since the
path can be set up oddly when running a cron script.  You also generally
want to redirect both standard out and standard error somewhere.
Otherwise, any output gets mailed to the user who has the cron job.  This
is a good thing in some cases, but not so good in a lot of others.
Instead of /dev/null, you could use the ">> /var/log/saytime.log" to
append output to that file.  In the case of "saytime" it seldom produces
output other than an error saying that the device is busy, so we don't
really need to log it.  Check out the default crontab file.  It has some
cron entries already set up.  As I said, this applies to just about any
Unix system you can think of.  Some crontab programs will have special
features like being able to understand symbolic names like MON instead of
the numbers, but I stick with the most portable format.

Here is another example.  suppose you leave your computer on 24 hours a
day.  That Saytime program is keeping you up all night!  How to shut it up
while letting it run in the daytime?  Try this.

0,15,30,45 8-20 * * * /usr/local/bin/saytime >/dev/null 2>&1

Now, the cron entry will only run from 8 A.M. to 8 P.M.  I think you get
the idea.  These are just examples I made up off the top of my head.

Lastly, always use "crontab -e" to edit cron files.  If you don't like the
editor it starts with, set the environment variable "EDITOR" to point to
your favorite editor.  For example "export EDITOR='pico -t'" or "export
EDITOR=emacs."  Since some programs use VISUAL instead of EDITOR, I have
my .profile export both variables and give them the same value.  This
effects most news readers, and most other email readers besides Pine which
has its own option for setting an editor.  I hope this was helpful.





[Index of Archives]     [Linux for the Blind]     [Fedora Discussioin]     [Linux Kernel]     [Yosemite News]     [Big List of Linux Books]
  Powered by Linux