How to check if a pid is alive?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check if a pid is alive?
# 1  
Old 06-28-2007
Question How to check if a pid is alive?

I want to do some operations provided the pid is active.
# 2  
Old 06-28-2007
I assume you have the pid with you.

Code:
while kill -0 $pid
do
  echo "$pid is alive"
  # do your operations
done

# 3  
Old 06-28-2007
In theory the only person who can reliably use a pid is the processes parent, as once a process dies, it's pid can get reused by another process. Then once wait/waitpid has returned that pid it is no longer valid.

In some systems pids range from 2 to 32767, if you had a webserver running one CGI program a second, then the pids would be reused every nine hours.
# 4  
Old 06-29-2007
Hey...we can also check in one line command rite?
ps -ef | grep <pid> | grep -v grep

Last edited by nithin03051985; 06-29-2007 at 04:19 AM..
# 5  
Old 06-29-2007
How to check if a pid is alive :: A simple approch

ps -p <PID>

If the process is alive then, this will return the process id with the time and command.

Smilie
# 6  
Old 06-29-2007
Quote:
Originally Posted by nithin03051985
Hey...we can also check in one line command rite?
ps -ef | grep <pid> | grep -v grep
Not really, say of you were look for pid 241, it would return a false positive on all those 2410<=x<=2419 and 24100<=x<=24199 and other combinations.

Quote:
Originally Posted by vino
while kill -0 $pid
This would only work if you owned the process.
# 7  
Old 06-29-2007
Try...
Code:
if ps -p $id > /dev/null
then 
    : active
else
    : inactive
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Pid file and process check

Hello, I am running ubuntu14.04 What I am trying to do is restart a process with a shell when pid is dead. I restored pid nr in a file and check with ps aux | grep -v grep | grep $(cat *.pid)| awk '{ print $2 }' While surfing on google, I have found an answer saying that restoring pid in a... (2 Replies)
Discussion started by: baris35
2 Replies

2. Shell Programming and Scripting

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... (16 Replies)
Discussion started by: mohtashims
16 Replies

3. UNIX for Dummies Questions & Answers

Check the URL is alive or dead with port number

Hi, I am running certain weblogic instance, in which it's hard to find which instance is stopped or running after stopping the weblogic , cause process status is for all the instance is same. A URL which shows the instance is stopped or running. But i have major challenge to check it through... (2 Replies)
Discussion started by: posix
2 Replies

4. UNIX for Dummies Questions & Answers

Check who is writing to a file? (pid)

Hello, is there a way to check who is writing to a file? (pid) Like someone starts a ftp transfer, is uploading a file and I'd need to know which pid is actually writing to it. Or is there a way to check which file a process is currently accessing? Greetings and thanks for all your... (4 Replies)
Discussion started by: TehOne
4 Replies

5. Shell Programming and Scripting

How to check whether server is up or down by monitoring PID?

we have a server, which becomes down at times and we dont get any notification mail. Can we monitor PID for that server run and create a script to send notification to a mail id? I have URL for the server, which I can use. Please let me know if more information is required from my end to... (2 Replies)
Discussion started by: vandana.parwani
2 Replies

6. Shell Programming and Scripting

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 (9 Replies)
Discussion started by: 1nsyz1on
9 Replies

7. UNIX for Dummies Questions & Answers

Check whether a process is alive or not

Hi Everybody I have small requirement that needs to be implemented in shell script. Currently i have shell script which invokes a java process say "Process A" which runs in background. If some one tries to invoke again the same shell script , then there should be some mechanism inside the... (23 Replies)
Discussion started by: appleforme1415
23 Replies

8. Shell Programming and Scripting

Script to check status of a PID

i'm just learning scripting, and have been banging my head against this I want to check if my WAS6 java process is running and if so..echo me a messages. If not then echo me a different messages the problem i have is I dont know how to represent a NULL return value. If i grep for a was6... (14 Replies)
Discussion started by: zeekblack
14 Replies

9. Shell Programming and Scripting

check if job still alive and killing it after a certain walltime

Hi! I'm using a script to start a process that might run forever if some parameters are given wrong (it's part of an optimization). I would now like to have the process killed after a certain walltime in that case. So far I get it done with the following lines ./My_process.e & pid=`ps -ef |... (3 Replies)
Discussion started by: ciwstevie
3 Replies

10. Shell Programming and Scripting

Pid still alive after "control C"

hi , i'm running 2 similar script (ksh) under AIX , the first go normally out when i type "control c" : ps before : notes01 19882 38990 0 11:54:48 pts/0 0:00 recover -s cla0bkpe -d /base/base01 -t May 05 23:02:17 2005 -v -f -a /base/base01/mail/mail-01/xxxx.nsf notes01 30866 40880 0... (4 Replies)
Discussion started by: Nicol
4 Replies
Login or Register to Ask a Question