Koozali.org: home of the SME Server

Crontab & redirection of stdout /sterr

John Crisp

Crontab & redirection of stdout /sterr
« on: May 01, 2003, 10:16:41 PM »
Hi,

I am tring to get a simple script to run from crontab but need some help on redirection. I have had a good read of the various howtos online but still can't get my head round it.

I would like to direct the standard reporting from the script to /var/log/messages and error messages to root or admin by mail.

Can someone explain or show me the correct syntax. Crontab line is as follows :

10 * * * * root /somedir/somescript {stdout > messages} {stderr > mail}

I have seen examples such as 2>&1 where stderr goes to the same place as stdout, but in my case I want notifcation of errors.

I presume it should be something along the lines of :

10 0 * * * root /somedir/somescript >1 /var/log/messages >2 mail -s Panic -u root

I just feel that there is probably a neater way of doing it.

B. Rgds
John

Bill Talcott

Re: Crontab & redirection of stdout /sterr
« Reply #1 on: May 02, 2003, 12:25:16 AM »
This is handy for a script or two of mine also...

http://www.fincher.org/tips/General/Unix.shtml
-----------------------------
0 is stdin; 1 is stdout; 2 is stderr

redirect std err to file
command 2>file

direct the output from command to stderr
commamd >& 2

direct std output and std err to foo or /dev/null
command >foo 2>>foo

same as above stderr rerouted to whatever filedes 1 is.
command >foo 2>&1
-----------------------------

I'm just as new (probably newer) to this as you, but from that I think this is what you'd want:

10 0 * * * root /somedir/somescript 1> /var/log/messages 2> mail -s Panic -u root

John Crisp

Re: Crontab & redirection of stdout /sterr
« Reply #2 on: May 02, 2003, 06:43:22 PM »
Thanks for that Bill.

I just found this :

http://unix.about.com/library/weekly/aa110600e.htm

I think that a single > overwrites a files and >> appends as below. I'll give it a whirl and let you know what happens. I am sure that there is something missing regarding the sending of a mail message - there must be a shorter way.

Thnaks for your input.

B. Rgds
John

cmd > file
Redirect stdout to a file. Overwrite the file unless noclobber is turned on.

cmd >| file
Redirect stdout to a file. Overwrite the file even if noclobber is turned on.

cmd >> file
Append stdout to a file.

cmd 2> file
Redirect stderr to a file. Overwrite the file unless noclobber is turned on.

cmd 2>| file
Redirect stderr to a file. Overwrite the file even if noclobber is turned on.

cmd 2>> file
Append stderr to a file.