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:

 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.

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.

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 :eek:

Regards,
Shantanu

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:

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:

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

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".