Operating System - HP-UX
1847817 Members
4894 Online
104021 Solutions
New Discussion

while loop not working with remsh in a bourne shell script

 
SOLVED
Go to solution
Boyd Kodama
Frequent Advisor

while loop not working with remsh in a bourne shell script

I wrote a script similar to the following one that root would run to remotely kill the processes owned by user, tester:

#!/usr/bin/sh
remsh 123.45.678.999 ps -ef | grep -v tester | grep -v grep | awk '{print $2}' |
while read pid
do
remsh 123.45.678.999 kill -9 $pid
done
# end of script

Well, it only kills the first process found, i.e. it appears that after it does the first remote kill it exits the while loop although there are other processes listed for $pid.

The "local" version of the script works:
#!/usr/bin/sh
ps -ef | grep -v tester | grep -v grep | awk '{print $2}' |
while read pid
do
kill -9 $pid
done

Any ideas why the `remsh` is causing/allowing the script to loop only once?

thanks
Without Mondays, there weren't be any Fridays
6 REPLIES 6
Tim Malnati
Honored Contributor

Re: while loop not working with remsh in a bourne shell script

The easy workaround to this behavior is to have the actual script reside in an executable file on the remote machine and then inkove it with remsh.
Paul Hite
Trusted Contributor
Solution

Re: while loop not working with remsh in a bourne shell script

Your problem is the line:
remsh 123.45.678.999 kill -9 $pid

You need to use:
remsh 123.45.678.999 -n kill -9 $pid

As it now stands, the first time the line is run, it sucks up all the remaining pid lines and sends them to the remote kill process which ignores them.
Albert E. Whale, CISSP
Honored Contributor

Re: while loop not working with remsh in a bourne shell script

Boyd,

the remsh -n simply redirects stdin from /dev/null.

You'll need to use the following format:

resmh -n -c "kill -9 $pid"

This will work.

Sr. Systems Consultant @ ABS Computer Technology, Inc. http://www.abs-comptech.com/aewhale.html & http://www.ancegroup.com
Boyd Kodama
Frequent Advisor

Re: while loop not working with remsh in a bourne shell script

Thanks!

Adding the "-n" to the line worked.

A co-worker also suggested the idea of kicking off via remsh a script on the remote systems that would kill the processes. There are 29 that other systems.

Adding the "-c" didn't work. I received a "sh: -c: not found"

Without Mondays, there weren't be any Fridays
Alan Riggs
Honored Contributor

Re: while loop not working with remsh in a bourne shell script

The -n is the correct solution. The reason it failed is that remsh will by default pull input from stdin. the while loop also relies on stdin. Whenever you use a remsh within a while loop you need to use teh -n flag.
Brian DeHamer
Advisor

Re: while loop not working with remsh in a bourne shell script

_
May the force be with you