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.