LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Is there something "more unique" than process id PID? (https://www.linuxquestions.org/questions/linux-general-1/is-there-something-more-unique-than-process-id-pid-714045/)

77nafets 03-24-2009 07:18 AM

Is there something "more unique" than process id PID?
 
Hi there!

I have the problem that I want to identify a process reliably after some time (i.e., after weeks or even months) to check whether it is still running or not.

This however seems rather difficult: I cannot check the PID, since PIDs are only unique during the lifetime of the process.

The maximum PID value is 32767, so when more processes are launched during uptime, the PID counter wraps around, starting from the beginning again. You can easily verify this with a shell script:

#!/bin/bash

if test -r "$0"; then
echo "pid=$$";
"$0"&
fi;

Name this 'wrap'. When launched, it will run until you remove/rename its file. While running, it repeatedly restarts itself, occupying a new PID. The output looks something like

> ./wrap
[...]
pid=32765
pid=32766
pid=32767
pid=300
pid=301
pid=302
[...]

I also cannot use a combination of PID and command, since the same program may have been relaunched, accidentally occupying the same PID. This is not very likely, but not impossible. Just see the 'wrap' script above.

Especially since the process I want to watch is to be relaunched repeatedly, it is very likely that it will have a PID assigned that it used before.

Is there something like a "totally unique" PID that's never reused during uptime? I guess a 64-bit number should be enough.

Any suggestins?

Thanks!

tronayne 03-24-2009 07:59 AM

You might look at PID, PPID, STIME and TIME in combination; have a look at what ps -ef shows you or ps -elf.

openSauce 03-24-2009 08:02 AM

Quote:

Originally Posted by 77nafets (Post 3486017)
Especially since the process I want to watch is to be relaunched repeatedly,

I don't understand what it is you want. If "the process" is relaunched repeatedly, each relaunch will be a separate process. Assuming the first one has come to an end, you already know the answer to your question without checking anything - no, the process is not still running.

77nafets 03-24-2009 09:33 AM

Quote:

Originally Posted by tronayne (Post 3486052)
You might look at PID, PPID, STIME and TIME in combination; have a look at what ps -ef shows you or ps -elf.

Yes, cool idea. I might use a combination of PID and starttime (e.g. see proc(5)). Thank you.

---

Quote:

Originally Posted by openSauce (Post 3486059)
Assuming the first one has come to an end, you already know the answer to your question without checking anything

No.

I start a process 'foo' in the background (using bash syntax).

> foo&
[1] 4321

It's easy to test whether a process with such a PID exists:

> kill -0 4321 && echo alive
alive

> kill -0 9999 && echo alive
bash: kill: 9999 - No such process

Now assume 'foo' terminates.
The PID counter wraps around.
Another 'foo' process is started, accidentally having the PID 4321 assigned.

> kill -0 4321 && echo alive
alive

Even looking in the process table will tell me that it's 'foo' running with PID 4321. But The original process died.

---

Any other suggestions?

Thanks,
Stefan

openSauce 03-24-2009 10:34 AM

Ah I see. Sorry, misunderstood you. Don't really know a good way to do this, but a quick-and-dirty way might be something like:

Code:

(starttime=$(date); touch $starttime-begun; foo; touch $starttime-finished)
The parentheses start a new shell in which to execute the commands, which (I'm hoping) would protect the $starttime variable from being unset in the intervening months. tronayne's way is probably better.


All times are GMT -5. The time now is 08:12 PM.