Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using ssh to run multiple commands

Status
Not open for further replies.

lazyrunner50

Programmer
Jul 30, 2004
63
0
0
US
I am trying to use ssh to log in to several linux boxes, check if the processes are running on each, and if a process is down on any one of the boxes, shut down all the processes on all the boxes, and then bring them all back up again.

Here's what I have so far:

Code:
ssh userName@remoteBox "num=`ps -ef | grep userName | grep -i processName | grep -v grep | awk '{print $2 }'`; echo $num"

The problem is, I can manualy ssh to the box, and run the command after ssh to get the process number, but I can't run the whole command in one line. Nothing is echoed out when I do that..

What I need is to be able to access $num outside of the call to ssh so that I can say something like

Code:
ssh userName@remoteBox "num=`ps -ef | grep userName | grep -i processName | grep -v grep | awk '{print $2 }'`; echo $num"
if [ $num ] ; then
      shutdownFlag=1
      echo "Shutdown flag is set!"
    fi
 
a simple way is to store a script on all the boxes. This script will be executed remotly by ssh command and the script will end by exit $num.

I use scripts this way to monitor lots of processes. On each boxe i have the same script library to monitor lots of things.
 
Yeah, I thought about that, and have done so for one part of the script, but it gets a little messy because while each box has it's own independent processes, they all share the same mounted directories. This means I would have a ton of "helper" scripts located under the same directory.
 
You do not need to do the filtering on the remote host. Just do the ps remotly and the rest locally :
Code:
num=`ssh userName@remoteBox 'ps -ef' | grep userName | grep -i processName | grep -v grep | awk '{print $2 }'`

--------------------

Denis
 
I tried that exact command (substituting in the appropriate variables for userName, remoteBox, and processName) and tried echoing the variable afterwards:

Code:
num=`ssh userName@remoteBox 'ps -ef' | grep userName | grep -i processName | grep -v grep | awk '{print $2 }'`
echo $num
but nothing showed up...

I am however, able to log into the box by running this
Code:
ssh userName@remoteBox
and then I get the process number when I do this
Code:
ps -ef | grep userName | grep -i processName | grep -v grep | awk '{print $2 }'
 
In order to isolate the problem:
Can you run just a single command with ssh, e.g.
Code:
ssh userName@remoteBox ps
hth
 
Code:
num=`ssh USER@MYIP 'ps -ef' | grep MYUSER | grep -i MYPROCESS | grep -v grep | awk '{print $2 }'`

I tried this, and echo $num gave me a PID. (in fact, it gave me several in the situation where there were several processes with same name, like mozilla in my case).

Are you sure you're choosing a user/process combo that exists?

-Haben sie fosforos?
-No tiengo caballero, but I have un briquet.
 
This is what I get:
Code:
$ cat testScript
#!/bin/ksh
num=`ssh userName@remoteBox 'ps -ef' | grep userName | grep -i processName | grep -v grep | awk '{print $2 }'`
echo $num
$ ./testScript

*** IT IS AN OFFENSE TO CONTINUE WITHOUT THE CORRECT AUTHORIZATION ***

Individuals using this computer system with or without proper authority are subject to having all of their activities monitored and recorded and should hav
e no expectation of privacy unless local law, regulation or contract provides otherwise.

Anyone using this system expressly consents to such monitoring and to all appropriate disclosure of any evidence of criminal activity to law enforcement of
ficials, as well as appropriate disclosure of any evidence of violation of the firm's rules, policies, procedures or standards of conduct to management.

Individuals using this computer system have a legal obligation to treat all information on the system as strictly confidential and not to disclose or allow
 such information to be disclosed to any third party.  This obligation survives any termination of employment with the firm.


$

But yet, when I run the commands individually, I get the process number (as expected):
Code:
$ ssh userName@remoteBox 
*** IT IS AN OFFENSE TO CONTINUE WITHOUT THE CORRECT AUTHORIZATION ***

Individuals using this computer system with or without proper authority are subject to having all of their activities monitored and recorded and should hav
e no expectation of privacy unless local law, regulation or contract provides otherwise.

Anyone using this system expressly consents to such monitoring and to all appropriate disclosure of any evidence of criminal activity to law enforcement of
ficials, as well as appropriate disclosure of any evidence of violation of the firm's rules, policies, procedures or standards of conduct to management.

Individuals using this computer system have a legal obligation to treat all information on the system as strictly confidential and not to disclose or allow
 such information to be disclosed to any third party.  This obligation survives any termination of employment with the firm.



Last login: Thu Jun 30 14:36:58 2005 from theBoxRunningTheScript

*** IT IS AN OFFENSE TO CONTINUE WITHOUT THE CORRECT AUTHORIZATION ***

Individuals using this computer system with or without proper authority are subject to having all of their activities monitored and recorded and should hav
e no expectation of privacy unless local law, regulation or contract provides otherwise.

Anyone using this system expressly consents to such monitoring and to all appropriate disclosure of any evidence of criminal activity to law enforcement of
ficials, as well as appropriate disclosure of any evidence of violation of the firm's rules, policies, procedures or standards of conduct to management.

Individuals using this computer system have a legal obligation to treat all information on the system as strictly confidential and not to disclose or allow
 such information to be disclosed to any third party.  This obligation survives any termination of employment with the firm.



$ ps -ef | grep userName | grep -i processName | grep -v grep | awk '{print $2 }'
21351
$
 
Hmm. I use BASH.. Try with another shell.

-Haben sie fosforos?
-No tiengo caballero, but I have un briquet.
 
Try this

num=`ssh userName@remoteBox "ps -ef | grep userName | grep -i processName | grep -v grep" | awk '{print $2 }'`
 
...just my two cents ...

Are you familiar with Expect? It's a Tcl-based tool for automating interactive programs ... works great for programs like ssh ... it's fairly easy and straightforward to get-up and running!
 
for what it's worth,

this...
ps -ef | grep tring

is the same as this...
ps -ef | grep string | grep -v grep

but more elegant and efficient.
 
I've been using Python to do this.

Eg.

#!/usr/bin/python
import commands

hostname = "xxx"
cmd="""ssh %s 'uname -a'""" % hostname
result = commands.getoutput(cmd)
print result



"If you always do what you've always done, you will always be where you've always been."
 
You may do a
Code:
pid=$(ps -o pid= -C processname)
but I don't know how to include the username in this query.
Code:
pid=$(ps -o pid= -U username)
will give you every process of user 'username', but the combination:
Code:
pid=$(ps -o pid= -U username -C processname)
will give you a combination of processes which belong to 'username', or are of name 'processname'.


seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top