1833867 Members
2054 Online
110063 Solutions
New Discussion

Re: ksh logging

 
SOLVED
Go to solution
Ratzie
Super Advisor

ksh logging

I am writing a script that is being executed by roots crontab and sends the output to a log.

0 0 * * 6 su back -c /home/back/scripts/backup/backup_db.sh > /home/back/log/backup_db.log.`date +\%j`

My problem is is that the script is in ksh, this script call 3 other scripts.

I would like to have all the output emailed to me, (and later, only email if I get an error from any of the scripts that ran).
How do I set logging in backup_db.sh and at the end mail the file.

I am sure there is a better way to do this, but any help would be appreciated.

All the scripts are done in ksh, yes, I would like to go to perl, but, not at this time...
4 REPLIES 4
Sridhar Bhaskarla
Honored Contributor
Solution

Re: ksh logging

Hi,

This is one of the solutions.

0 0 * * 6 su back -c /home/back/scripts/backup/backup_db.sh > /home/back/log/backup_db.log.`date +\%j` 2>&1; mailx -s "Details of backup" your_id@yourdomain.com < /home/back/log/backup_db.log.`date +\%j`

The above will mail all the details.

Later for only error log

0 0 * * 6 su back -c /home/back/scripts/backup/backup_db.sh > /home/back/log/backup_db.log.`date +\%j` 2>/home/back/log/backup_db.err.`date +\%j`; mailx -s "Details of backup" your_id@yourdomain.com < /home/back/log/backup_db.err.`date +\%j`

-Sri



You may be disappointed if you fail, but you are doomed if you don't try
A. Clay Stephenson
Acclaimed Contributor

Re: ksh logging

The key to what you are trying to do is the "exec" command.

For example,
exec 1>/var/tmp/mylog 2>&1

ls
echo "Test"
ls Badfile

will combine stdout and stderr onto /var/tmp/mylog. Put the exec at the top of your script and stdout/stderr will be sent to the file. At the end of your script test to see whether mylog is non-empty and if so mail the contents of that file and then remove it.
If it ain't broke, I can fix that.
Bill Hassell
Honored Contributor

Re: ksh logging

There isn't a single solution to your question. cron will automatically mail you a copy of all stdout and stderr from your script(s). Now since you redirected stdout into the /home/back/log directory, the email is limited to stderr. ksh is not a problem, just standard shell programming techniques. Just use tee in your cron job:

su back -c /home/back/scripts/backup/backup_db.sh | tee /home/back/log/backup_db.log.`date +\%j`

What tee does in this context is to write stdout to the file (just like before) but also echo everything written to the file to stdout so it will be emailed. As far as all output versus only errors, you control this in your script(s). Set a variable, perhaps ERRORONLY to be tested and echo or print information only when it is false. To test everything, set ERRORONLY to true and you should see only errors (that is, text sent to stderr in your script). Set it to false and you'll see every echo and print sent to stdout.


Bill Hassell, sysadmin
Todd McDaniel_1
Honored Contributor

Re: ksh logging

I am really curious why you arent using posix... HPUX posix shell is very good and IMHO superior to KSH.
Unix, the other white meat.