I have an Ubuntu Server 10.04 machine on which I run a number of testing instances of a server daemon, each running on a different IP address. I've named the services by the last octet in the address: 'myserver40', my'server41', etc., up to 'myserver59' At any given time I may be running any number of these services, but not know which ones.
I need to find which services are running and also make a list of server numbers (the IP octet) so that after doing some file and directory maintenance I can restart the servers that were running. I know it probably involves using ps and grep, but I'm at a loss how to use them.
My initscript to start/stop the different instances of the server takes two arguments: <IP_octet> <command>.
Code:
# Stop servers
for i in {40..59}; do
servername=myserver$i
# ps and grep ????
# If server i is running, stop it
if [ ? ]; then
/etc/init.d/myserver $i stop
serverlist="$serverlist $i"
fi
done
# Perform file and directory mainenance
...
# Restart servers that had been running
for i in $serverlist; do
/etc/init.d/myserver $i start
done