Finding Out When A Process Has Finished?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Finding Out When A Process Has Finished?
# 1  
Old 10-31-2001
Finding Out When A Process Has Finished?

Problem

I have an application which basically runs lots of UNIX programs remotely, using the Telnet protocol. For each program it remotely executes, it stores the process ID (PID) for that process.

At regular intervals, I would like my application to take the PID for every process still running and find out if it is still running, or if it has terminated.

Solution

What is the most elegant, or the cleanest way of finding out whether a process is still running? Bear in mind that it is a program doing the checking, not a human!

My idea was simply to execute a "ps -p <PID>" command for every PID and if there are no entries, the process has terminated, otherwise it must still be running.

What do you think? Thankyou very much.
# 2  
Old 10-31-2001
Yes , with ps -ef you can check the proccess fine. Also consider 'top'. It is an application that will tell you alot about applications running.
# 3  
Old 10-31-2001
ps and top require a nontrivial amount of resources to run. And then you still must parse the output. The cheapest way to see if a process is running is via the kill() system call. If you call kill with signal set to zero, error checking is performed but no signal is actually sent.

This works from the shell as well. You can do "kill -0 $pid" and then check the return code. If it worked, the process is still alive.
# 4  
Old 11-01-2001
Thanks for your tips! I have one more query concerning PIDs:

On a typical UNIX machine (whatever that is!), how often is a specific PID likely to be allocated to a process? Of course, this will depend on the number of processes running, but I was just curious to see if there is a chance that during the time between my checks to see if processes are running, one of my processes terminates and it's PID is actually applied to a new process, thus giving my application the impression that the terminated process is still running, when in fact it has finished & the PID being searched for actually belongs to a completely different process!

My application is currently checking all PIDs at 5-minute intervals, so in the worst case scenario, the same PID would have to be allocated within a time-frame of 4:59 minutes. This seems unlikely.
# 5  
Old 11-01-2001
Pid's in unix go up to 32,000 then they recycle. In a multi-processor environment it is common for each cpu to grab 5 or 10 pid's at a time to reduce spinlocking so you can't expect them to be allocated in order.

I hope that you are not running this as root! Assuming that you are running as an ordinary user there is no problem. If another user allocates the same pid, your process will not succeed in invoking a kill system call against it. You can only kill your own processes unless you're root.
# 6  
Old 11-02-2001
No problems - I am connecting to and running all commands as a standard UNIX user.

I have one final query concerning PIDs and use of the ksh ! variable :

90% of the time, when I parse the output of a "echo $!" command, I correctly obtain the PID number. However, sometimes the value of the ! variable is echoed back as:

"[1] 12766"

where 12766 is the PID.

Does anyone know why i get this strange prefix? I will have to change the way I parse the echo of the ! variable now, because I only want the PID, not the bracketed number 1.

many thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding a file process ?

Hi, I am trying to find a file that have a different name than it should be processing, the file name is ( Fifa15 ) is there a command to use? I got that file by ps -ef | grep fifa15 but how do I know what is running ? thanks a lot, I am learning unix so sorry if that is a... (2 Replies)
Discussion started by: latinooo
2 Replies

2. Shell Programming and Scripting

Finding process which ended another process

Hello, The scenario is as follows, I have a background process running initially for which i know the PID on machine1. I use ssh from machine 2 to execute a script in machine 1. For some reason the back ground process is terminated. I would like to know which process caused the... (6 Replies)
Discussion started by: prasbala
6 Replies

3. UNIX for Dummies Questions & Answers

Finding a rogue process

Afternoon all, hopefully someone can give me a hand with this (the following may be explained very poorly :rolleyes: ) I know there's a process running on one of our Solaris 10 boxes that runs approximately every 5 minutes. Unfortunately I've no idea, who owns it, what it is called, or how it is... (2 Replies)
Discussion started by: dlam
2 Replies

4. UNIX for Advanced & Expert Users

Finding process id of subsequent process

hi all, I am trying to find the process id of the subsequent process created via fork and exec calls in perl. For eg: envVarSetter dataCruncher.exe < input.txt > output.txt When I fork and exec the above command, it returns only the pid of envVarSetter and I don't know how to find the... (9 Replies)
Discussion started by: matrixmadhan
9 Replies

5. Shell Programming and Scripting

Finding the process id of the process using the ports

Hi Any idea how to get the process id of the process using the ports lsof -i :portnumber does not work in my machine. I am on sun Solaris SPARC. Any suggestion is highly appreciated (1 Reply)
Discussion started by: kinny
1 Replies

6. Linux

Need help in finding process

Hello, Iam running a apache webserver in CentOS recenlty a hacker has attacked my server using RFI attack and did something in my server.. After that everyday at 8Pm my httpd is using about 5000 pid's actually in normal it takes only about 30 - 40 pid's. and also exim uses 2000 pid's totally my... (2 Replies)
Discussion started by: dheeraj4uuu
2 Replies

7. Shell Programming and Scripting

finding Background Process Id

Hi Gurus, How can i find background process is completed or not. I have mentioned my scenario below. Actually Pr1 Process is running in back ground, i just want to know whether this process completed or not. I can come to know the process id by typing pid=$! but i want to trigger... (4 Replies)
Discussion started by: krk_555
4 Replies

8. UNIX for Dummies Questions & Answers

Finding out process id in a scipt

Hi, If in a shell script i write a command ls > bla & ls The output is redirected to bla and the next ls starts as first one is going on in background. I want to find the PID of the first command. Thanks in advance (2 Replies)
Discussion started by: vibhor_agarwali
2 Replies

9. UNIX for Dummies Questions & Answers

finding process id

is there a way to find the process id of a process because i have same process invoked several times. when i need to kill them, i get confused with the id. Thanks, sskb :( (8 Replies)
Discussion started by: sskb
8 Replies
Login or Register to Ask a Question