Solved

writing script to check that a process is running

Posted on 2001-06-04
6
261 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
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 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…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

705 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

18 Experts available now in Live!

Get 1:1 Help Now