Link to home
Start Free TrialLog in
Avatar of kyh74
kyh74

asked on

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.
Avatar of SEve
SEve

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
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
ASKER CERTIFIED SOLUTION
Avatar of yuzh
yuzh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kyh74

ASKER

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".
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
Avatar of kyh74

ASKER

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

thank all for the help.