Results 1 to 3 of 3
Hi there, please help me with this issue.
What I want to do:
I want to check the log of a process in server A. If by grep I found ...
- 11-29-2007 #1Just Joined!
- Join Date
- Nov 2007
- Posts
- 2
statements after "ssh" line won't get executed?
Hi there, please help me with this issue.
What I want to do:
I want to check the log of a process in server A. If by grep I found certain keywords, I know a bad event occurred. In that case I want the script to send a mail to me.
Since server A does not have sendmail, but server B does. So I put this script at server B, and every 5 minutes it ssh into server A to do what I want it to do. Here is my script:
For testing purpose, I did not run it at background. and sleep time was shortened, checking condition was negated. So I expected it to dangle there and in a few seconds it should send me a mail.Code:while (true) do ssh root@server.A echo `hostname` cd /var/log/ res=`tail -300 proc.log.$(date +%Y%m%d%H) | grep 'rcv failed' | wc -l` #echo $res exit # I want the script exited server at this step if [ $res -ne 0 ] then echo "`date`" | mail -s "process is is dead" me@domain.com fi sleep 300 done
But each time when I ran it, it immediately returned to prompt - and showed me as a terminal at server A, displayed me the last login time into this server, and it stopped there. I did not see it exit server A and return to server B. and I did not get the mail.
Now in this terminal, if I type "exit", it will really quit from server A and return to server B, and show me the debugging print outs.
and the line "echo `hostname`" always gave me the name of server B, as if it had ever been to server A.
And it complains that it could not find the log file I want it to check - that means this statement was not executed until I manually exited from server A.
Basically, I conclude that as soon as the script succeeded the ssh line, it considers itself as a process in server A, and is unable to reach those statements after the ssh line. Therefore these statementes won't be executed - until I manually exited from server A. But this is not the behavior I expect.
So I wonder how I can achieve my goal? Is there a way to force the script process to "carry all statements" over to server A and continue executing them in there?
If my description is too messy, please let me know and I'll try to clarify. Any tips will be much appreciated.
Larry
- 11-29-2007 #2
You're invoking an interactive ssh session. That's not going to work in a script.
An alternate suggestion, with examples from my box. (Different log file, different expression, but you get the idea.)
See the manpages for ssh(1) for details about using ssh user@host command.Code:#!/bin/bash YUMLOG=$(ssh mrbig@fugu tail -10 /var/log/yum.log) echo ${YUMLOG} | grep -o 'clamav' | wc -l exit 0
- 11-29-2007 #3Just Joined!
- Join Date
- Nov 2007
- Posts
- 2
works like a charm
Hi anomie, I modified my code according to your hint and now it works exactly as I expect. Thank you so much for the help! Have a great day!
Larry


Reply With Quote