Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

writing script to check that a process is running

Hi, I am very new to Unix and i would to write a script to stop and start a daemon. I know how to do that portion but i would like the script to also check that daemon has been stop before excuting the start command. I know manually the command "ps -ef|grep daemon" can check for running daemon. May i know how can i incorporate this portion in my script.Thank you.
0
kyh74
Asked:
kyh74
1 Solution
 
SEveCommented:
under cshell:

#!/sur/bin/csh -f

set result = `ps -ef|grep daemon`
if ("$result" == "") then
  # no such process
  do your stuff
else
  # process is not stopped
  another stuff
endif

seve
0
 
newmangCommented:
You may want to use the ps -e without the -f switch. I have found that in some cases if the path to the process is too long the name of the process is not displayed by the ps and the grep then fails. It caught me a couple of times.

Cheers - Gavin
0
 
yuzhCommented:
Hi SEve ,

   Here's a sample of a start up script: (I use sshd as an example)
#=============================================
#!/bin/sh
#
 # Generic script used to start and stop a service

 case "$1" in

  'start')
         # Start the sshd server
       if [ -f /usr/local/sbin/sshd ]; then
                /usr/local/sbin/sshd &
        fi
        ;;
  'stop')
      pid=`ps -ef | grep sshd | grep -v grep | awk '{print $2} ' `        
      if test "$pid" ;  then
       /usr/bin/kill $pid
      else
             echo "No PID file. Process may not be running"
      fi
 ;;
 *)
     echo "Usage: /etc/init.d/sshd { start | stop }"
     ;;
 esac
 exit 0
#======================================================

   Here's a scripe to check if the deamon is running:
#
#!/bin/sh
      pid=`ps -ef | grep sshd | grep -v grep | awk '{print $2} ' `        
      if test "$pid" ;  then
           echo " The sshd is runing"
      else
             echo "No PID file. Process may not be running"
             /etc/rc2.d/S50sshd start
             echo "sshd restart ....!"
      fi
exit 0
#================================================

I hope this scripts can help.



0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
kyh74Author Commented:
Hi SEve, I have try something similar to this but "ps -ef|grep daemon" command will always return value. Even there is no such daemon running it will also return the value "grep daemon".
0
 
SEveCommented:
hi kyh74, let's change the command to :
ps -ef | grep daemon | grep -v grep
that means exclude all lines which include word grep, i.e. the process which we ran to find your daemon. this will help, seve
0
 
kyh74Author Commented:
The command
"ps -ef | grep sshd | grep -v grep | awk '{print $2}"
solve the problem

thank all for the help.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now