Solved

writing script to check that a process is running

Posted on 2001-06-04
6
269 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

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.

Question has a verified solution.

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

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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.

617 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