Check if PID exists

In a Shell Script what is the most generic way to find in the PID exists or not.

If it does not exist how can I echo the user "PID does not exist" & terminate the unix script ?

If the command can work on most flavors of operating system the more useful I will find it to be.

Current system is HP-UX.

Thank you for your suggestions.

u can use

ps -ef | grep PID | grep -v grep

1 Like

you can

echo $$

to know the pid of the script you executed

Hi, this is a way:

ps -ef|awk '{printf "%s\n",$2;}'|grep -q -w $MYPID

then check the $? variable: if 0 the pid exists, else not exists.

1 Like

I dont want PID of the script but i need to check the PID i pass to my script.

Can you also tell me how do I come out of the script if the PID does not exist ?

[[ pgrep "process_name" ]] && echo "Process Present" || echo "Process is not present"

Try with pgrep if it not works then just replace fist bracket command with other suggested option in this thread.

The problem is that it is populating the script that i am executing as i pass the PID to the script the "ps" command wrongly thinks that the process exists when it does not.

Below is the out of my ps -ef | grep <PID> | grep -v grep command

 
 
ps -xef | grep  | grep 15160 | grep -v grep;
 
 
bea 25845 14840  0 05:28:04 pts/7     0:00 /bin/sh ./Master_Script.sh weblogic 15160 /tmp/user_projects/domains/mydomain /tmp/ system password

The output should be blank becoz there is no process as 15160.

Kindly suggest.

Try with awk then..

ps -ef | awk '$2==15160'
1 Like

If your OS has the /proc filesystem (most modern ones do) you can also test like this

if [ -d /proc/$PID]
then
      echo "Process is still running"
else
     echo "Process has stopped"
fi

Edit: above withdrawn, (HP-UX dosn't have /proc). You could get ps to check for you:

if ps -p $PID > /dev/null 2>&1
then
    echo "Process is still running"
else
    echo "Process has stopped"
fi
1 Like

Assuming the variable pid contains your pid and the user that executes the command has the right privileges:

kill -0 $pid 2>/dev/null && 
  printf '%d exists\n' $pid || 
    printf '%d does not exist\n' $pid
1 Like

In my opinion, if existence is all that's to be tested, this is by far the sanest suggestion in this thread. There's no point in generating extensive ps output only to search for something that was already known before ps was invoked (the pid).

radoulov's suggestion should be used, however, if what really matters is whether or not a process can be terminated by the effective user id running the shell script.

Regardless, there's no need for any ps | [grep | awk] pipelines.

Regards,
Alister

HI u can use below idea

please use correct syntax below may not be excat sytntax but u will get and idea

c=`ps -ef | grep exactprocessname | grep -v grep|wc -l`

if [ c -gt 0 && c -lt 1 ]

echo "process is running"

elif

if [ c -gt 1]

echo "multiple instances of process running"

else
echo "processs not running"
fi

That can't be used because what is given is the pid and not the process name. And, even if what was known were the process name, that approach leaves the door open to false positives, for example, if the process name matches a user name or an argument name, or matches some other field in the ps output. If process name were the key, pgrep is the best solution.

The suggestion in post #2 is similarly deficient.

Regards,
Alister

Since pid is reused, it is better to look for having a log file open using fuser. The pid could exit and the new pid assigned to another process of yours that has nothing to do with the application. Check not only the pid and user but the application, at least.

the u can try pls correct me if im wrong

ps -ef | awk $2 ~/somepid/ '{print $2}' >> tmp

c=`wc -l tmp`

the check 

if [ c -gt 0]
then 
echo " process exists"
else
echo " doesnot exist"


What if the pid is 345 and it doesn't exist but there is a different process running with a pid of 2345? In that case, a false positive.

Why not just use ps -p pid and be done with it?

Regards,
Alister

Checkpid, user, appname:

pc=`ps -fp $pid | grep -c ' $USER .*appname"`