LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 03-24-2009, 07:18 AM   #1
77nafets
LQ Newbie
 
Registered: Mar 2009
Posts: 2

Rep: Reputation: 0
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!
 
Old 03-24-2009, 07:59 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 2,860

Rep: Reputation: 697Reputation: 697Reputation: 697Reputation: 697Reputation: 697Reputation: 697
You might look at PID, PPID, STIME and TIME in combination; have a look at what ps -ef shows you or ps -elf.
 
Old 03-24-2009, 08:02 AM   #3
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Rep: Reputation: 39
Quote:
Originally Posted by 77nafets View Post
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.
 
Old 03-24-2009, 09:33 AM   #4
77nafets
LQ Newbie
 
Registered: Mar 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by tronayne View Post
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 View Post
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

Last edited by 77nafets; 03-24-2009 at 09:44 AM.
 
Old 03-24-2009, 10:34 AM   #5
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Rep: Reputation: 39
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.
 
  


Reply

Tags
fork, kernel, processes


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ns:"error when calling class OldSim"&tclsh:"invalid command+child process exits abn." shojaru Linux - Newbie 0 03-05-2009 04:23 AM
Finding pid of a process opened by "popen" Guttorm Programming 2 03-12-2007 02:35 PM
function for getting pid of "any" process nice_indian Programming 2 01-12-2005 09:54 AM
using bash to find the pid of a process called "name" poiuytrewq Linux - Newbie 3 10-25-2004 09:01 PM
Assigning "unique name" to "Extraversion" when upgrading Kernal MatthewASmith79 Linux - Newbie 5 08-26-2004 08:31 AM


All times are GMT -5. The time now is 03:58 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration