Running a function on a remote server via SSH in a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Running a function on a remote server via SSH in a script
# 1  
Old 12-01-2009
Running a function on a remote server via SSH in a script

I'm working on a script (mostly for practice) to simplify a task I have to do every now and then. I have a cluster with 6 servers on it, each server has a directory with a set of files called *.pid and *.mpid. Each file contains the pid of a process that may or may not be running on that server. I'm trying to create a script that will hop to each server, collect what pid/mpid files there are, and then check to see if that process is actually running or not.

I've gotten it running using a pretty simple (albeit ugly) one liner:

Code:
 for i in 1 2 3 4 5 6 ;do echo "SYS0$i"; ssh sys0$i 'for i in `ls /usr/local/isa/run/`;do x=$(ps -ef|grep `cat /usr/local/isa/run/$i`|grep -v grep);if [[ $x = "" ]];then echo "$i is not running";else echo "$i is running";fi;done';done
SYS01
SYS02
BmsUI.mpid is running
BmsUI.pid is running
CORBAnameservice.mpid is running
CORBAnameservice.pid is running
CORBAnotificationservice.mpid is running
CORBAnotificationservice.pid is running
SYS03
ServiceGateway.mpid is running
ServiceGateway.pid is running
ETC

However, I'd like to be able to create a prettier looking script to do the same thing. I thought for neatness sake I could just put all of the logic in a function at the beginning of the script and then ssh to each server and call on the function, but that doesn't seem to work.

Code:
root@bms-nycnm-srv02:/root/procCheck# cat procCheck
#!/bin/bash
check ()
{
for file in `ls /usr/local/isa/run`
do
proc=$(ps -ef|grep `cat /usr/local/isa/run/$file`|grep -v grep)
if [[ $proc = "" ]];then
echo "$file has a pid/mpid file, but is not running"
else
echo "$file is running"
fi
done
}
for i in 1 2 3 4 5 6
do
echo "SYS0$i"
ssh sys0$i 'check'
done

I assume the issue is that once the script jumps to a new server and starts a subshell, it forgets about what ever functions I've declared in the first place. Any help or advice I'd appreciate it.
# 2  
Old 12-01-2009
Hi,
I tried by replacing the single quotes (') with a (`) and it did seem to be able to find the function but then it was having trouble executing the commands in the called function Smilie

Regards,
Shantanu
# 3  
Old 12-01-2009
I tried that, but then it just seems to throw out an error related to the value of the function. As an example, this is what happens if I create the function check just in the shell on one system and run it:

Code:
root@bms-nycnm-srv02:/root/procCheck# type check
check is a function
check () 
{ 
    for file in `ls /usr/local/isa/run`;
    do
        proc=$(ps -ef|grep `cat /usr/local/isa/run/$file`|grep -v grep);
        if [[ $proc = "" ]]; then
            echo "$file has a pid/mpid file, but is not running";
        else
            echo "$file is running";
        fi;
    done
}
root@bms-nycnm-srv02:/root/procCheck# check
BmsUI.mpid is running
BmsUI.pid is running
CORBAnameservice.mpid is running
CORBAnameservice.pid is running
CORBAnotificationservice.mpid is running
CORBAnotificationservice.pid is running
root@bms-nycnm-srv02:/root/procCheck#

But when I run it in the script with the backticks as you suggested I get this:

Code:
root@bms-nycnm-srv02:/root/procCheck# ./procCheck
SYS01
bash: BmsUI.mpid: command not found
SYS02
bash: BmsUI.mpid: command not found
SYS03
bash: BmsUI.mpid: command not found
SYS04
bash: BmsUI.mpid: command not found
SYS05
bash: BmsUI.mpid: command not found
SYS06
bash: BmsUI.mpid: command not found

# 4  
Old 12-01-2009
You can't do that since your function somehow has to make it to the other side otherwise the code can not get executed. You could use a "here document".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ssh - running commands on remote server

Hi, I am trying to run commands on a list of servers that I can ssh to and just want to know if there is a 'cleaner' way of doing this. At the moment, I am doing as below. Is there a way that I can escape the double quote differently? If a use a single quote to enclose the commands that I... (1 Reply)
Discussion started by: newbie_01
1 Replies

2. Shell Programming and Scripting

Running Commands on a Remote Linux Server over SSH

Hello, I'm trying to create a ksh script to ssh to a remote server, enter the password and a couple commands. For security reasons I have changed the login, password and ip in my example. #!/bin/ksh ssh -t -t username@12.5.5.3 << EOF password cd bin pwd EOF When I run it. It... (5 Replies)
Discussion started by: seekryts15
5 Replies

3. Shell Programming and Scripting

How to Append the output of a script running in remote server to a file in local server?

Hi guys, So i am in server1 and i have to login to server 2, 3,4 and run some script there(logging script) and output its result. What i am doing is running the script in server2 and outputting it to a file in server 2 and then Scp'ing the file to server1. Similarly i am doing this for other... (5 Replies)
Discussion started by: srkmish
5 Replies

4. Shell Programming and Scripting

Script to ssh to remote server

Hi All, I need to prepare a script. Description: Currently i am in server "x(ubuntu os)", here i need to develop a script to ssh to another server "y(ubuntu os)", i have password less authentication to "y". i have done the below #!/bin/bash #ssh to the server "y" and confirming i am... (2 Replies)
Discussion started by: kumar85shiv
2 Replies

5. Shell Programming and Scripting

Running script on remote server

Hi All, I need to run a ksh script on around 200 servers(consisting of AIX,LInux,HP-UX,Solaris). The script is there in the /tmp directory of all the servers. I want want to execute the script in background on the respective servers and then exit from there. I have written something like below:... (8 Replies)
Discussion started by: proactiveaditya
8 Replies

6. Shell Programming and Scripting

Problem running ssh from remote server

So I have a script which performs some basic commands on another server via ssh. It works great, no issues at all. Let's call this "Script A" BUT, this working script is to be executed remotely from a different UNIX script on another server, also by ssh. Let's call this "Script B". When... (1 Reply)
Discussion started by: newerakb
1 Replies

7. Shell Programming and Scripting

Running a Script in a Remote server

I am trying to write a script that would let me run a command in a remote server using ssh. scriptA: (dcm2nii is a command that only works on the other server) dcm2nii a b c scriptB: (I run this one on the current server) ssh -X otherserver /home/abc/Desktop/scriptA But when I do ... (2 Replies)
Discussion started by: ZeroGPX
2 Replies

8. Shell Programming and Scripting

Script to check running processes on remote server.

Hi, I am trying to write a script, which queries a db to get the names of processes, stores it in a file and then checks if that process is running on a remote server. However I am not getting it right, could anyone help me out. #!/bin/sh echo "select Address from Device where Cust =... (5 Replies)
Discussion started by: amitsayshii
5 Replies

9. Shell Programming and Scripting

Running a remote Server through perl script

Hello people, I am want to run a server on remote machine through perl scripting using telnet api. Now when I try to do so, the server gets started perfectly, but as soon as I close the telnet connection in the script, the server started on the remote machine suddenly goes down. I also... (0 Replies)
Discussion started by: chandrak
0 Replies

10. Shell Programming and Scripting

running a script on remote server.

I need to run a script on a remote server from my ksh script. The issue I'm having is that I need to logon to the remote server as a different user. (see the following) logged on to server 1 as adsmgr neet to log on to server 2 as odemgr run passwd_util.ksh Thanks in advance. (1 Reply)
Discussion started by: whited05
1 Replies
Login or Register to Ask a Question