Check process running Status with PID

Good day

I am fairly new to Shell Scripting.

I want a script to check if a process is up by checking the process's PID and then return a value for when it's running which would be 0. If it isn't running it should give any other value that 0.

Any Help is appreciated

Regards

Try:

ps -ef | grep -q process_name
echo $?   # will be 0 if found, or else non-zero value

Hi

Thanks but it says

ps -ef | grep -q dsdm
grep: illegal option -- q
Usage: grep -hblcnsviw pattern file

FYI this is SUN OS

Regards

Try:

ps -p $PID >&-

-or perhaps-

ps p $PID >&-

ps varies from system to system.
See what

man ps |grep -i pidlist

says

E.g.

PID=<nr>
if  ps -p $PID >&- ; then
  echo running
else
  echo not running
fi

On Sun OS, you can use /usr/xpg4/bin/grep

From manuals in Sun OS

Sorry for being such an idiot when it comes to this.

PID=<999999>
if ps -p $PID >&- ; then
echo running
else
echo not running
fi

When I run the above file it passes no matter what number i put into PID?

I basicaly need a batch file which our tool uses to execute on remote unix servers, which reads the output value of 0 or whatever.

Oh I'm sorry with

PID=<nr>

I mean e.g.

PID=99999

Still passes with whatever value I put in there scrutinizer.

:frowning:

OK what happens when you enter these two commands? On my system:

$> if  ps -p 99999 >&- ;then echo running; else echo not running; fi
not running
$> if  ps -p 1 >&- ;then echo running; else echo not running; fi
running

This simple script will do it:

kill -0 $pid