Agnello George wrote: > #!/bin/sh -x > > if [ $(tac /var/log |grep -e "error: syswrite()" | wc -l ) = 0 ] ; then
This can be improved. Let's walk through it. On my system /var/log is a directory of log files and not an actual log file. The typical log file is /var/log/syslog one many systems and /var/log/messages on others and yet slightly different paths on different systems. I will assume that you are not getting an error message from that invocation above and on your system it really is a file at /var/log but it would be good to verify this. Didn't you say you wanted to check /var/log/spamd.log? 'tac' is 'cat' in reverse. 'tac' concatenates and prints files in reverse. It does this by reading the file into memory and then walking through the memory image in reverse. If the size of the image is large enough then the file is copied to a temporary file. The /var/log file is usually large enough to need a temporary file. This is very inefficient. In fact since the grep is going to walk through the entire file it doesn't need to do so in reverse. if [ $(tac /var/log |grep -e "error: syswrite()" | wc -l ) = 0 ] ; then Is the same as the following. This removes an large inefficiency in the check. if [ $(grep -e "error: syswrite()" /var/log | wc -l ) = 0 ] ; then Next let's look at grep itself. 'grep something file | wc -l' will count the number of lines. But here you only care if it is zero or nonzero. That can be done more efficiently with 'grep -q'. In the case of -q since grep knows that it is a zero or nonzero occurrence it can optimize and stop as soon as it knows the answer. Therefore the above can be improved by using this: if ! grep -q "error: syswrite()" /var/log; then Let's now move to the collection of lines in the script using this new test. if ! grep -q "error: syswrite()" /var/log; then exit 1 else echo "your mailserver is down" |mail -s " pls check server ip 216.185.xxx.xxx " [EMAIL PROTECTED] fi Using 'exit 1' indicates an error. But actually there is no error in that case. It should be 'exit 0' instead. Also since this is an exit the following parts past that in the script will not be executed. It is considered good programming style to exit in the early part of the 'if' statement and then not use an 'else' part in that case. But usually this is done for error conditions and not for okay conditions. Lets reverse the condition and try that here. Here is a potentially final version of the script. if grep -q "error: syswrite()" /var/log/spamd.log; then echo "your mailserver is down" | mail -s " pls check server ip 216.185.xxx.xxx " [EMAIL PROTECTED] exit 1 fi exit 0 This way the script exits 0 (success) when there are no errors and exits 1 (error) when errors exist in the logfile. This is a typical way to program these checks. You might also consider using 'mailq' to check your mta status. > then i add a crontab to run for ever 10 min > > crontab -e > 10 * * * * /your/location/of/script As Eddy Beliveau pointed out this should be */10 using Vixie cron syntax to run every ten minutes. But if your cron is not a Vixie cron but is instead a traditional cron then you would need to list out all of the minutes. 0,10,20,30,40,50 would run every ten minutes using the older cron syntax. > if there is an easier way kindly tell me !!! Hope this helps, Bob