Solved

writing script to check that a process is running

Posted on 2001-06-04
6
266 Views
Last Modified: 2010-04-21
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
Comment
Question by:kyh74
6 Comments
 
LVL 3

Expert Comment

by:SEve
ID: 6152273
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
 
LVL 4

Expert Comment

by:newmang
ID: 6154459
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
 
LVL 38

Accepted Solution

by:
yuzh earned 50 total points
ID: 6154678
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:kyh74
ID: 6154728
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
 
LVL 3

Expert Comment

by:SEve
ID: 6155144
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
 

Author Comment

by:kyh74
ID: 6155550
The command
"ps -ef | grep sshd | grep -v grep | awk '{print $2}"
solve the problem

thank all for the help.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question