Help with Scriptoutput and mailing the log


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with Scriptoutput and mailing the log
# 1  
Old 09-25-2009
Help with Scriptoutput and mailing the log

Hi Guys,

i´ve oncemore a little question, hope somebody can help me.

Here´s my "problem":
I´ve running a script via cron every evening at 21:30 - this script
reads 5 xml-Files and output the result in a logfile.
When the script ends without errors i´ve an entry in my logfile and also when the script ends with an error.

Here´s this script:
Code:
#! /bin/sh
#
cd /srv/www/htdocs/import
/bin/echo "--------------------------------------------"
/bin/date
echo Script "Name"
 
ausgabe="$(/usr/bin/php5 script1.php 2>&1)";
status=$?;
#echo "$ausgabe";
echo -n 'Script1 was read - ';
[[ $status -eq 0 ]] && [[ -z "$ausgabe" ]] && echo -n Success || echo -n Error - ;
echo " $ausgabe";
 
ausgabe="$(/usr/bin/php5 script2.php 2>&1)";
status=$?;
#echo "$ausgabe";
echo -n 'Script2 was read - ';
[[ $status -eq 0 ]] && [[ -z "$ausgabe" ]] && echo -n Success || echo -n Error - ;
echo " $ausgabe";
 
ausgabe="$(/usr/bin/php5 script3.php 2>&1)";
status=$?;
#echo "$ausgabe";
echo -n 'Script3 was read - ';
[[ $status -eq 0 ]] && [[ -z "$ausgabe" ]] && echo -n Success || echo -n Error - ;
echo " $ausgabe";
 
ausgabe="$(/usr/bin/php5 script4.php 2>&1)";
status=$?;
#echo "$ausgabe";
echo -n 'Script4 was read - ';
[[ $status -eq 0 ]] && [[ -z "$ausgabe" ]] && echo -n Success || echo -n Error - ;
echo " $ausgabe";
 
ausgabe="$(/usr/bin/php5 script5.php 2>&1)";
status=$?;
#echo "$ausgabe";
echo -n 'Script5 was read - ';
[[ $status -eq 0 ]] && [[ -z "$ausgabe" ]] && echo -n Success || echo -n Error - ;
echo " $ausgabe";

and here´s the Logfile:

when everything is okay:
Code:
Thu Sep 24 21:30:01 CEST 2009
Script Name
Script1 was read - Success
Script2 was read - Success
Script3 was read - Success
Script4 was read - Success
Script5 was read - Success

and when something went wrong:
Code:
Tue Sep 22 21:30:01 CEST 2009
Script Name
Script1 was read - Error - You have an error in your SQL syntax; check the manual that corresponds to your MyS
QL server version ... bla bla ....2009', kng = 'T', segnr1 = 'G', segname =
 '' at line 1
Script2 was read - Success
Script3 was read - Success
Script4 was read - Success
Script5 was read - Success

and here´s my crontab:
Code:
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.XXXXiQy98H installed on Thu Sep 24 14:56:51 2009)
# (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)
30 21 * * *  /root/script.sh >> /var/log/scriptlog.log

Now i want that respectively the entry from the last day in this logfile will be sent via mail to root.
Perhaps something like this:
cat the logfile, grep the last entry, put it in a temp-file, send this file via mail and delete it then ...

but i´ve no idea how to do this ...

Parhaps, can somebody help me?
Thanks in advance ...
# 2  
Old 09-25-2009
Quote:
Originally Posted by jackcracker
Now i want that respectively the entry from the last day in this logfile will be sent via mail to root.
Perhaps something like this:
cat the logfile, grep the last entry, put it in a temp-file, send this file via mail and delete it then ...

but i´ve no idea how to do this ...

Parhaps, can somebody help me?
Thanks in advance ...
Could you explain more on this if you want a mail of the log or last line from the log

Here are couple of ways you could do that. Notice I have put round bracket around the script ( ) ...

Code:
#! /bin/sh
#
( cd /srv/www/htdocs/import
/bin/echo "--------------------------------------------"
...
...
echo -n 'Script5 was read - ';
[[ $status -eq 0 ]] && [[ -z "$ausgabe" ]] && echo -n Success || echo -n Error - ;
echo " $ausgabe"; ) >> /var/log/scriptlog.log

# Not sure if you need this ... check Last 10 line for error ..
tail 10 /var/log/scriptlog.log  | grep 'Error' > /tmp/error
[ -z /tmp/error ]  && mail -s 'Errors' youremail@whatever < tmp/error
rm -rf /tmp/error

# 3  
Old 09-25-2009
[quote=chakrapani;302356280]Could you explain more on this if you want a mail of the log or last line from the log

Here´s are the last lines of my log-file:
Code:
...
...
...
--------------------------------------------
Tue Sep 21 21:30:01 CEST 2009
Script Name
Script1 was read - Error - You have an error in your SQL syntax; check the manual that correspon ... bla bla ds to your MyS
.2009', kng = 'T', segnr1 = 'G', segname =
 '' at line 1
Script2was read - Success
Script3was read - Success
Script4was read - Success
Script5was read - Success
--------------------------------------------
Wed Sep 22 21:30:01 CEST 2009
Script Name
Script1was read - Success
Script2was read - Success
Script3was read - Success
Script4was read - Success
Script5was read - Success
--------------------------------------------
Thu Sep 23 21:30:02 CEST 2009
Script Name
Script1was read - Success
Script2was read - Success
Script3was read - Success
Script4was read - Success
Script5was read - Success
--------------------------------------------
Thu Sep 24 21:30:01 CEST 2009
Script Name
Script1was read - Success
Script2was read - Success
Script3was read - Success
Script4was read - Success
Script5was read - Success

Now i want only the entry from Thu, Sep 24 send via mail ... only these 7 lines, because when i sent the whole logfile i´ve to scroll very long time to get to the last - interesting - entry ... the one from last Day.

I try the following :
Code:
tail -n 10 /var/log/scriptlog.log > /tmp/error && mail -s 'Errors' root < /tmp/error

This works but the mail is unformatted - do i´ve a chance to format it, so that the mail looks like the same as my logfile ...
every entry is among each other?
# 4  
Old 09-25-2009
You can change the last line to this:
Code:
tail -$(grep -n  date +"%d %b" /var/log/scriptlog.log | head -1 |awk -F: '{ print $1 }') /var/log/scriptlog.log | mail -s ... bla bla ..

Please change it .. if you find that I have not too complicated it ... the idea is

grep -n .. show me the line numbers of where is today date is
head -1 ... give me only one date ... just incase your php script also decide to put in date ..
awk -F ... now get only the number ... you could use cut or something .. I like awk ..

tail ... the starting one .. -<number> will get me from there to end of the file.

pipe mail bla bla ... same as earlier ...

So I get only lines from today's date till end and get mailed to me ... About formatting of the email ... is it your mail client misbehaving ? or do you check mail directly from /var/spool/mail/<userid> ... or whereever you mail is stored ..

Last edited by chakrapani; 09-25-2009 at 09:05 AM..
# 5  
Old 09-25-2009
I think this is very complicated ...
i think it´s better to leave the first version:

Code:
tail -n 10 /var/log/scriptlog.log > /tmp/error && mail -s 'Errors' root < /tmp/error

Then it´s unformatted but never mind ...
Ok, formatted will be nicer but so long
The mail will be send via Postfix and i use Outlook 2003


Another idea is to sent the file "error"in /tmp as an attachment via mail

Code:
Code:
tail -n 10 /var/log/scriptlog.log > /tmp/error && mail -s 'Errors' -a /tmp/error root

... but when i type this at console i get no return ... and no mail ... and i wonder why.
i can stop this only with ctrl+c twice

Last edited by jackcracker; 09-25-2009 at 08:58 AM.. Reason: forget something
# 6  
Old 09-25-2009
Code:
tail -n 10 /var/log/scriptlog.log > /tmp/error && mail -s 'Errors' -a /tmp/error root

What is -a option ? is it for attachment ?
Also the complicated method suggested will get only line for the day ... Say you get error and error is more than 10 lines ... then also you will get complete log for the day ...
Just a suggestion .. Smilie

Also was wondering why we have && ... usually && are expanded as
( some command ) && (and then run this ) || ( or its me who will be called ) ..
# 7  
Old 09-25-2009
... yes, -a is for attachment (man mail)

ok, then i´ll try the complicated method,
but how must the complete syntax be?

Code:
tail -$(grep -n  date +"%d %b" /var/log/scriptlog.log | head -1 |awk -F: '{ print $1 }') /var/log/scriptlog.log | mail -s 'Errors' root < /tmp/error

i get return
Code:
grep: +%d %b: No such file or directory
tail: invalid option -- /

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Mailing the job log

Hi All, i am a begginner in autosys. i want the job log to be mailed if a job gets failed in autosys.please help me with this (0 Replies)
Discussion started by: sahe
0 Replies

2. UNIX for Advanced & Expert Users

Regarding mailing of logs

Hi Folks, I have an application which logs get continuously refreshed and made after every 1 second , I want that those logs get mailed to me at my email id sara@somewhere.com The location is cd /var/log/abc/log Please advise the command so that I can mail abc.log at my mailid inbox... (2 Replies)
Discussion started by: SankalpS
2 Replies

3. UNIX for Dummies Questions & Answers

Regarding mailing of logs

Hi Folks , I have one query I traverse to a location where logs are being made daily, Now I want that a particular log in that logs directory to be get mailed to me automatically at a particular time on daily basis at my mail id, Please advise the command for that.. for ex the directory is... (7 Replies)
Discussion started by: SankalpS
7 Replies

4. Shell Programming and Scripting

Mailing Script

Hi, I am totally New to This Scripting Area.We have developed some reports in Bo and User need those reports to be sent through Unix Server (SFTP). Can any one provide me script for transfering File from one location to other with the requirement like if that file fails to reach destination... (4 Replies)
Discussion started by: Sashanth_S
4 Replies

5. Shell Programming and Scripting

HTML mailing

Hi All, I have a comma separated file as below. file-1 ---------- a,b,c d,e,f g,h,i . . . I want to send the above file in Mail in HTML format with all the values of each column in separate cells i.e. the body of the mail should be in tabular format & each field should be separate... (2 Replies)
Discussion started by: 46019
2 Replies

6. Shell Programming and Scripting

Mailing script

Hi, I have a file lets say FILE1. FILE1 ------ name,age charlie,25 harry,29 david,32 Pls help me writing a mailing script.... which will mail the content of file in the body of the mail & it will look something like below in two columns. name age charlie 25 harry 29... (1 Reply)
Discussion started by: 46019
1 Replies

7. Shell Programming and Scripting

Mailing Problem

I have a Unix server with SunOS 5.8 installed on it. I have set a cron job which will send mail across the two different networks. As of now i can only send mails to the abc.com who owns the server but would also like to send it to xyz.com is there any setting or parameter wherein i need to add... (2 Replies)
Discussion started by: nimish
2 Replies

8. UNIX for Dummies Questions & Answers

Mailing an attachment

Hi, I need to compress (zip) a flat file and mail the same as an attachment. I had tried the following piece of code. But when i open the attachment in mail, the data is incorrect (displays all junk characters). tar cf abc.tar abc.txt compress abc.tar uuencode abc.tar.Z abc1.wri | mailx... (4 Replies)
Discussion started by: gayath3
4 Replies

9. Shell Programming and Scripting

Mailing an attachment

Hi, I need to compress (zip) a flat file and mail the same as an attachment. I had tried the following piece of code. But when i open the attachment in mail, the data is incorrect (displays all junk characters). tar cf abc.tar abc.txt compress abc.tar uuencode abc.tar.Z abc1.wri | mailx -s... (0 Replies)
Discussion started by: gayath3
0 Replies

10. UNIX for Dummies Questions & Answers

Mailing an attachment

Hi, I'm fairly new to using Unix and I was just wondering can someone please tell me the simplest way to mail and attachement using the mail command using Solaris? I have tried using uuencode file name | mail johndoe@ttp.ie but it hasn't worked. I have been mailing the file to myself using... (4 Replies)
Discussion started by: gerwhelan
4 Replies
Login or Register to Ask a Question