Register
It is currently Tue Oct 21, 2014 7:35 am

Determine if a process is running, by name


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Wed May 19, 2010 3:44 pm   

Joined: Wed May 19, 2010 3:03 pm
Posts: 1
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



Top
 Profile  
 PostPosted: Thu May 20, 2010 2:49 pm   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
What about using killall?
Code:
if killall -0 myserver$i ; then
  # running
  ...
fi


Top
 Profile  
 PostPosted: Wed Jun 22, 2011 7:33 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Code:
if pgrep myserver$i > /dev/null; then
    # stop it
fi


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP