View Single Post
Old 09. Dec 2009, 04:04 AM   #2 (permalink)
debtboy
Senior Member
 
debtboy's Avatar
 
Join Date: May 2009
Location: ~/
Posts: 128
Default

Now lets go over the crontab file...
The overall default crontab file consists of a few directives, (shell, path, mail, home variables, etc...)
and then the scheduling and command sections.

The scheduling section looks a little intimidating at first, but it's very simple if you have a legend to go by.
Code:
 ---------------------- minute (0 - 59)
 |
 |   ------------------ hour (0 - 23)
 |   |
 |   |   -------------- day of month (1 - 31)
 |   |   |
 |   |   |   ---------- month (1 - 12) or jan, feb, mar, apr, may...
 |   |   |   |
 |   |   |   |   ------ day of week (0 - 6) (Sunday=0 or 7)
 |   |   |   |   |      (or sun, mon, tue, wed, thu, fri, sat)
 |   |   |   |   |
 |   |   |   |   |
 *   *   *   *   *   <user> <command>
This format:
Directives
<schedule> <user> <command> is valid for the overall default crontab file /etc/crontab, but a user's personal crontab has a much simpilar format.

No directives
<schedule> <command>

use a single space between parts of the schedule and the command.

Here is the overall default crontab file on my system:



Now for some scheduling examples:

* 02 * * * means every day at 2 O'Clock
* */02 * * * means every 2 hours

01 * * * * (The first minute of every hour)
*/05 * * * * (every 5 minutes)

20 05 * * * (at 5:20 AM)
* * 29 * * (29th of every month)

* * * * 1-5 (Monday thru Friday)
* * */02 * * (every 2 days)

01 22 12 1 * (January 12th @ 10:01 PM)


In the command section you will enter a command or script to be executed.
For example, let's use the "date" command, the date/time output could be appended to a local file like this:
date >> /home/debtboy/log1.txt

To create a personal crontab file...
Open a shell and type "crontab -e" then enter you cronjob.

As you can see from the image, I set a job (appending the date output to log1.txt file) to run on a 2 minute interval so every 2 minutes a newline get added to the file.



After saving, everything is already working, no need to restart anything.

Now I've decided to add a second cronjob to the file so I follow the same procedure, open a shell and type crontab -e and add the new cronjob.



In this case a different file log2.txt will be appended on a 10 minute interval.
__________________
[I][B]Linux, the choice of a GNU generation[/B][/I]
debtboy is offline   Reply With Quote