Find exact creation time of a process; not kill a wrong process

Find exact creation time of a process; not kill a wrong process

Post by Michael Wa » Sat, 22 Nov 2003 13:29:31



(1)
Is there a portable way in Shell, Perl, other tools to find
the exact number of seconds since epoch of a process?

timestamp on /proc/<pid> disqualifies as it is not portable,
Linux /proc/<pid> is always current time.

"current time - elapsed time from ps" disqualifies as
current time and elapsed time from ps are taken at different
times, it may be off by a few seconds.

(2)
How can I kill a process without worrying killing a wrong process?
For example,

some_process & pid=$!
kill $pid

The $pid may be a recycled new process.

I am building an application used on space shuttle, and need
100% reliability. Please help.

Thanks.
--

 
 
 

Find exact creation time of a process; not kill a wrong process

Post by Ed Morto » Sat, 22 Nov 2003 14:27:21



> (1)
> Is there a portable way in Shell, Perl, other tools to find
> the exact number of seconds since epoch of a process?

Write the current time to a log file when you start the process.

Quote:> timestamp on /proc/<pid> disqualifies as it is not portable,
> Linux /proc/<pid> is always current time.

> "current time - elapsed time from ps" disqualifies as
> current time and elapsed time from ps are taken at different
> times, it may be off by a few seconds.

> (2)
> How can I kill a process without worrying killing a wrong process?
> For example,

> some_process & pid=$!
> kill $pid

> The $pid may be a recycled new process.

Use Job Control. man jobs.

Quote:> I am building an application used on space shuttle, and need
> 100% reliability. Please help.

And you're using a shell script for process monitoring??? Good luck and
don't tell the astronauts ;-).

        Ed.

Quote:> Thanks.


 
 
 

Find exact creation time of a process; not kill a wrong process

Post by Beard » Sat, 22 Nov 2003 17:36:50




>> (1)
>> Is there a portable way in Shell, Perl, other tools to find
>> the exact number of seconds since epoch of a process?

> Write the current time to a log file when you start the process.

>> timestamp on /proc/<pid> disqualifies as it is not portable,
>> Linux /proc/<pid> is always current time.

>> "current time - elapsed time from ps" disqualifies as
>> current time and elapsed time from ps are taken at different
>> times, it may be off by a few seconds.

>> (2)
>> How can I kill a process without worrying killing a wrong process?
>> For example,

>> some_process & pid=$!
>> kill $pid

>> The $pid may be a recycled new process.

> Use Job Control. man jobs.

And set pidmax to 999999 in /etc/system ;-)

Quote:>> I am building an application used on space shuttle, and need
>> 100% reliability. Please help.

> And you're using a shell script for process monitoring??? Good luck and
> don't tell the astronauts ;-).

And the OP alluded to portability to Linux??? Yikes! Could be the first
Compaq Deskpro in orbit ;-)
 
 
 

Find exact creation time of a process; not kill a wrong process

Post by Nils Petter Vaskin » Sat, 22 Nov 2003 18:27:46





>>> I am building an application used on space shuttle, and need
>>> 100% reliability. Please help.
>> And you're using a shell script for process monitoring??? Good luck and
>> don't tell the astronauts ;-).
> And the OP alluded to portability to Linux??? Yikes! Could be the first
> Compaq Deskpro in orbit ;-)

Well he didn't say it was critical for the survival of anyone, perhaps
failure will merely ruin some experiment so they'll have to do it over.

How much does a shuttle launch cost did you say?

--
NPV

"the large print giveth, and the small print taketh away"
                                Tom Waits - Step right up

 
 
 

Find exact creation time of a process; not kill a wrong process

Post by Beard » Sat, 22 Nov 2003 18:45:15






>>>>I am building an application used on space shuttle, and need
>>>>100% reliability. Please help.

>>>And you're using a shell script for process monitoring??? Good luck and
>>>don't tell the astronauts ;-).

>>And the OP alluded to portability to Linux??? Yikes! Could be the first
>>Compaq Deskpro in orbit ;-)

> Well he didn't say it was critical for the survival of anyone, perhaps
> failure will merely ruin some experiment so they'll have to do it over.

> How much does a shuttle launch cost did you say?

I didn't say anything about the cost of a shuttle launch, and do you
know what emoticons are used for?
 
 
 

Find exact creation time of a process; not kill a wrong process

Post by Nils Petter Vaskin » Sat, 22 Nov 2003 20:47:11



> I didn't say anything about the cost of a shuttle launch, and do you
> know what emoticons are used for?

I know you didn't.
I know what they are used for.

Do you know that some people make jokes without feeling the need to
indicate so with an emoticon?

Didn't mean any harm and I thought it obvious that I was continuing the
joke, it sems I was wrong, sorry.

--
NPV

"the large print giveth, and the small print taketh away"
                                Tom Waits - Step right up

 
 
 

Find exact creation time of a process; not kill a wrong process

Post by Beard » Sat, 22 Nov 2003 22:07:42




>>I didn't say anything about the cost of a shuttle launch, and do you
>>know what emoticons are used for?

> I know you didn't.
> I know what they are used for.

> Do you know that some people make jokes without feeling the need to
> indicate so with an emoticon?

> Didn't mean any harm and I thought it obvious that I was continuing the
> joke, it sems I was wrong, sorry.

Nils, please don't apologise for *my* sense of humour failure ;-) I had
a cranky 24 hours; worse than for a number of years :-( *My* apols to you...
 
 
 

Find exact creation time of a process; not kill a wrong process

Post by Michael Wa » Sun, 23 Nov 2003 02:29:50


Quote:>> (1)
>> Is there a portable way in Shell, Perl, other tools to find
>> the exact number of seconds since epoch of a process?

>Write the current time to a log file when you start the process.

Most of the time, we do not have control of starting the process.
For example, I want to find the creation time of process 1, 2, 3, 4.
Besides, "write a log file" and "start the process" are two instances
occuring at different time.
--

 
 
 

Find exact creation time of a process; not kill a wrong process

Post by Darren Dunha » Sun, 23 Nov 2003 03:19:09



Quote:> "current time - elapsed time from ps" disqualifies as
> current time and elapsed time from ps are taken at different
> times, it may be off by a few seconds.

"A few seconds" seems unlikely.  What kind of precision do you need?

$first = time;
$elapsed = `ps -o etime -p $PID`;
$last = time;

If first == $last, then do your calculation and you'll be accurate to
the second.  If not, try again.  I ran that several times in a perl
script and never had a second boundary appear.

Quote:> (2)
> How can I kill a process without worrying killing a wrong process?
> For example,
> some_process & pid=$!
> kill $pid
> The $pid may be a recycled new process.

Outside of the job control suggestion, I'm unaware of any way to refer
to a process by other than the PID, and there's no way I know of to
guarantee there's no race condition there.

However, you could probably put some bounds on things on a system where
PIDs are generated sequentially.  Fork a process to generate a PID and
see if it is within some range of your target PID.  If the distance is
large, you can be reasonably certain the PID will not be reused in the
time it takes to send the signal.

If the distance is small, then you have to make a decision about whether
you could delay the signal until the current PID passes the running
process PID.  If there's some other mechanism that would be useful here,
that would be cool, but I don't know what it is.

Best would be if the signal didn't actually kill the process but made it
do a lookup for some other data, verified that it's the one in question,
and then killed itself.  Then the race condition doesn't occur.

--

Unix System Administrator                    Taos - The SysAdmin Company
Got some Dr Pepper?                           San Francisco, CA bay area
         < This line left intentionally blank to confuse you. >

 
 
 

Find exact creation time of a process; not kill a wrong process

Post by Michael Wa » Mon, 24 Nov 2003 00:23:01




Quote:>Best would be if the signal didn't actually kill the process but made it
>do a lookup for some other data, verified that it's the one in question,
>and then killed itself.  Then the race condition doesn't occur.

The lookup does not need to be done if the signal is delivered
to the _right_ process. The lookup can not be done if the signal
is delivered to the _wrong_ process. It seems.

--

 
 
 

Find exact creation time of a process; not kill a wrong process

Post by Kjetil Torgrim Homm » Tue, 25 Nov 2003 02:48:19


[Michael Wang]:

Quote:

>   The lookup does not need to be done if the signal is delivered to
>   the _right_ process. The lookup can not be done if the signal is
>   delivered to the _wrong_ process. It seems.

do you know the parent reliably?

  pgrep -P parent | grep "^what I think the pid is$"

or use ps -o ppid.

if you control the child process totally, you can even have it hold
open a unique file, and use fuser(1m) to find it.
--
Kjetil T.                       |  read and make up your own mind
                                |  http://www.cactus48.com/truth.html

 
 
 

Find exact creation time of a process; not kill a wrong process

Post by Darren Dunha » Wed, 26 Nov 2003 01:51:10



Quote:> [Michael Wang]:

>>   The lookup does not need to be done if the signal is delivered to
>>   the _right_ process. The lookup can not be done if the signal is
>>   delivered to the _wrong_ process. It seems.
> do you know the parent reliably?
>   pgrep -P parent | grep "^what I think the pid is$"
> or use ps -o ppid.

There's still a race between when you issue the pgrep/ps and when any
signal you send is delivered.

Quote:> if you control the child process totally, you can even have it hold
> open a unique file, and use fuser(1m) to find it.

Same problem.

--

Unix System Administrator                    Taos - The SysAdmin Company
Got some Dr Pepper?                           San Francisco, CA bay area
         < This line left intentionally blank to confuse you. >

 
 
 

Find exact creation time of a process; not kill a wrong process

Post by Darren Dunha » Wed, 26 Nov 2003 01:56:48





>>Best would be if the signal didn't actually kill the process but made it
>>do a lookup for some other data, verified that it's the one in question,
>>and then killed itself.  Then the race condition doesn't occur.
> The lookup does not need to be done if the signal is delivered
> to the _right_ process. The lookup can not be done if the signal
> is delivered to the _wrong_ process. It seems.

Well, I was imagining that both programs were under the control of the
OP, so that both could have the logic written to do the checking.

If you had a signal that would be ignored by most processes, but could
be caught by your program, and would not be used otherwise by the
system, then that would also work for the more general case.

--

Unix System Administrator                    Taos - The SysAdmin Company
Got some Dr Pepper?                           San Francisco, CA bay area
         < This line left intentionally blank to confuse you. >

 
 
 

Find exact creation time of a process; not kill a wrong process

Post by Michael Wa » Mon, 08 Dec 2003 15:40:54




Quote:

>>   The lookup does not need to be done if the signal is delivered to
>>   the _right_ process. The lookup can not be done if the signal is
>>   delivered to the _wrong_ process. It seems.

>do you know the parent reliably?

>  pgrep -P parent | grep "^what I think the pid is$"

>or use ps -o ppid.

I know the parent, but a pid's ppid may change. For example, if parent
dies, the pid is adopted by init. The pid's name can also change.

The question is how can we identify a process besides pid, given a pid
can be recycled, and process creation time can not be obtained with one
step and the system clock can drifts.

I start to accept there is no absolute reliable programing; there is
only "good enough" programing. But there has to be a way to repeatable
reading a process's creation time, instead of current time minus
elapsed time; and a portable way in Unix.
--

 
 
 

Find exact creation time of a process; not kill a wrong process

Post by Ian Fitche » Mon, 08 Dec 2003 20:40:42



> But there has to be a way to repeatable reading a process's creation
> time, instead of current time minus elapsed time; and a portable way

 But perhaps there isn't?

 Maybe you should accept that, like with file creation times, you're
 just not destined to know unless you record it yourself.

Cheers,

        Ian

 
 
 

1. How to Kill a process and its sub-process and its sub-process at a time..

Hi,

        I have a problem in killing all the processes under a process.
        When I try to kill a Parent process, Child process and its
        child process are not being killed. I do manually by checking
        'ps -ef |grep process' and then kill each process and its child
        process individually. It some times consumes lot of time for me
        and also sometimes I will be missing some sub-processes.

        Can  any one help me in issueing a shell command or writing a
        shell script.

        Thank you all in advance for your time.

Thanks & Regards,

*******************************************************************

2. Q: PGX32

3. How to Kill Process and Its sub-process and its sub-process??

4. Need RH4.2 boot/root fd. to boot pcmcia/scsi ext. hd.

5. How can kill all child processes without killing parent process ?

6. Help with apache setup

7. How to kill process which not killed by 'kill' ?

8. Problem with Connection to Linux host

9. killing a shell script is not killing child processes

10. how to kill process when kill -9 is not working ?

11. killing a shell script is not killing child processes

12. A method to kill process that cannot be killed even with `kill -9'.

13. Parent process also terminating after killing child process in SIGINT