|Crons in Unix|
| 8:01 am on Dec 5, 2001 (gmt 0)|
How do u go about setting up a cron Job?
Like,how the crontab file is used & all related stuff...Thanx guyz
| 11:33 pm on Dec 8, 2001 (gmt 0)|
"crontab" is also an executable you can use to adjust your cron entries.
"crontab -l" to list your entries
"crontab -e" (on most flavors) to edit them. Rarely should you directly edit /var/cron/tabs/* or /var/spool/cron/crontabs/* or whereever they are kept on your flavor of unix.
The fields are described in one of your manpages, but manpages aren't easy for novices to parse, and digging through them to find out which one has the descriptions is even more complicated. In NetBSD, "man 5 crontab" gives you details of the file. In Solaris, "man crontab" gives you a good description. linux should be one of these two.
The entries on the crontab are simple (compared to other things UNIX-related). They are space- or tab-delimited (use space or tab to separate them), the first 5 describe times and the 6th field is the command you want to run.
The time fields are (in order):
field allowed values
day of month 1-31
day of week 0-6 (0 is Sun)
Use `*' to indicate "any".
Some examples from my desktop Solaris box:
: saturn; sudo crontab -l
10 3 * * 0 /usr/lib/newsyslog
20 * * * * /usr/lib/sendmail -q
1 1 * * 0 /usr/lib/makewhatis /usr/share/man
21 3,9,16,23 * * * /usr/bin/rdate dasher.warthog.com > /dev/null 2>&1
The first one runs "/usr/lib/newsyslog" every sunday at 3:10am. This rotates a given list of logfiles in /var/adm.
The second runs "/usr/lib/sendmail -q" every hour at 20-past-the-hour. This merely flushes out any e-mail that was spooled locally.
The third one builds the apropos database for the system manpages every Sunday morning at 1:01a. (Makes the index for you to use when you type "man -k ..")
The fourth one syncs the time on my computer against another computer called "dasher.warthog.com". It does this every day at 3:21a, 9:21a, 4:21p, and 11:21p.
The last one brings up a good point about crontab entries. If the program you run spits out any output, you'll get this intimidating e-mail from cron that looks like all h*ll broke loose, when it's actually just e-mailing you the output of your program. Using "> /dev/null 2>&1" throws any and all output away ("in the bitbucket" as we call it) so you'll never be pestered.
If you _want_ to receive e-mails from your program, then instead of the "> /dev/null..." stuff, append:
"2>&1 ¦ /usr/bin/mail -s 'my cron entry' email@example.com"
.. to the end of your line.
(For Solaris, use "/usr/ucb/Mail" instead of "/usr/bin/mail").
One last note. Pay attention to where you use "2>&1". It goes *before* `¦' or *after* `>'. There's a logical reason for this, but it'll take a day of well-thought-out typing for me to describe it. (This is the character sequence for /bin/sh [what cron uses to run crontab entries] that describes "include errors in this also.")
Hope this helps.
Rob++ (UNIX geek, at large)
| 12:06 am on Dec 11, 2001 (gmt 0)|
Jeez, windsor! That was spectacular! Thank you. :)
| 1:56 pm on Dec 11, 2001 (gmt 0)|
This kind of info is what makes WMW great! One of my web hosts told me to set up a "cron job" if I wanted to save my logs before they got trimmed... I had resigned myself to buying a UNIX book and figuring out what the heck a cron job was, and how to set it up... and here comes Windsor with the gory details! Great info, thanks!
| 7:56 am on Dec 13, 2001 (gmt 0)|
Great post, windsor!
A small related question, to save me some time just in case you know the answer to that one easily:
any idea which command I should use if I want to do a weekly cron job that saves my domain's log file to my web space with another name, just before it is overwritten? I don't need the time fields part, you've explained that perfectly, just the rest :)
| 4:48 pm on Dec 14, 2001 (gmt 0)|
So to reword your question, you wish to "rotate your logfile", yes?
If so, you're in luck. How much luck depends on which OS you're running. :-)
Let me come to the point from left field..
As you may know, there's a daemon ("little helper application that runs constantly") called "syslog" that throws into a file (or files) the noise produced by various parts of the operating system.
These files are typically located in /var/log (or /var/adm for Solaris). As you can guess, some of them get big, fast. You want to keep the files controllable, and sometimes compress them to save space while keeping them around. It has become standard practice to take the current file and rename it "file.0" and create a new "file" for syslog to write to. If you already have a "file.0", it'll move that to "file.1". Pretty soon, you get a stack of files that are "file", "file.0", "file.1", "file.2", ... (or "file", "file.0.gz", "file.1.gz", "file.2.gz", ... if you gzip them for compression).
We call that process "rotating log files".
NetBSD ships with a tool called "newsyslog". It has a /etc/newsyslog.conf to define the files, their permissions, compress-yes/no, etc.
Solaris ships with a rather weak script of the same name (that I just happened to have rewritten).
I've been told that linux has something that'll do that, "if you've installed the right package."
To the point, you should find what the system uses (if anything) to rotate the logfiles in /var/log or /var/adm. Most likely, you can adapt that to rotate your webserver logfiles.
If that proves to be futile, then we can easily adapt my /bin/ksh newsyslog script to do exactly what you need.