Google

Crontab

Crontab

Contact:[email protected]

Crontab Guide
By: Vincent Hillier [email protected]

----------------------------------------------------------------------------
----

Well, one of the greatest things in the computing world has to be
automation, and by automation, I mean tasks being performed with no user
interaction. In this guide, I'll cover the basics of Crontab.

Crontab allows you to schedule commands/scripts to be run at certain times.
For example, if you have a command that needs to be run once per week, and
you always forget to do it (SHOOT! boss hates that doesn't he/she? :)) you
can keep your boss and yourself happy, by simply putting those commands in a
cron job.

Ok, well to start out I'll cover the flags that can be passed to crontab.

The flags I will use in this guide are...

-e - edit
-l - list
-d - delete

Ok, so say you want to add a crontab, that will list all processes on the
system, and email you the results every hour. You would run crontab -e (The
default cron job editor is vi, if you don't like vi, set the VISUAL
enviroment variable to your editor of choice. For ex. if you like pico you
would type...)

export VISUAL=pico

Back to adding the crontab, first thing we do is open crontab...

crontab -e

Now we add our crontab, the syntax for this is as follows...

MIN HOUR DAY MONTH DAYOFWEEK COMMAND

So if you want to report the running processes on the system every hour,
something like this would work...

0 * * * * /bin/ps aux | mail username


That would send an email with the processes listed from ps to username every
hour. Of course you would replace username with your user name.

What if you wanted to run a command on the first day of every month?
Something like this would work...

1 0 1 * * /bin/ps aux | mail username

Which would do the same as the above example, except only once per month.

What if you wanted to run a script, monday to firday, at 5pm (end of the
work day) but the script has very verbose output. NOTE: any output from a
crontab running will be sent to you via email. So we want to run this
command every week day, at 5pm, and pipe the output to /dev/null. The below
example would work...

0 17 * * mon-fri /home/vince/script >> /dev/null 2>&1

That would run /home/vince/script daily during weekdays at 5pm and send all
the output to /dev/null. If you would like the output mailed to you, you
could use |mail username or just leave it blank.

Well that wraps this guide up, I hope you can find some useful ways to have
crontab benefit you.

If you have any comments, problems, or suggestions please post them to our
forums at http://www.lansystems.com/forum/index.php (preferred) or email me
directly at [email protected]. If you email me, regarding a guide, I will
post it, and my reply to the forums. This benefits everyone, if someone has
the same question/problem as you.


Back to the Index