Solved

writing script to check that a process is running

Posted on 2001-06-04
6
263 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

929 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now