Professional Web Applications Themes

Find exact creation time of a process; not kill a wrong process - UNIX Programming

(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 ...

  1. #1

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

    (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.
    --
    Michael Wang * http://www.unixlabplus.com/ * com
    Michael Guest

  2. #2

    Default Re: Find exact creation time of a process; not kill a wrong process



    Michael Wang wrote:
     

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

    Use Job Control. man jobs.
     

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

    Ed.
     

    Ed Guest

  3. #3

    Default Re: Find exact creation time of a process; not kill a wrong process

    Ed Morton wrote: 
    >
    >
    > Write the current time to a log file when you start the process.

    >
    >
    > Use Job Control. man jobs.
    >[/ref]

    And set pidmax to 999999 in /etc/system ;-)
     
    >
    >
    > And you're using a shell script for process monitoring??? Good luck and
    > don't tell the astronauts ;-).
    >[/ref]

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

    Beardy Guest

  4. #4

    Default Re: Find exact creation time of a process; not kill a wrong process

    On Fri, 21 Nov 2003 08:36:50 +0000, Beardy wrote: [/ref]
     [/ref][/ref]
     [/ref]
     

    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

    Nils Guest

  5. #5

    Default Re: Find exact creation time of a process; not kill a wrong process

    Nils Petter Vaskinn wrote: [/ref]
    >
    > [/ref]
    >
    > [/ref]
    >

    >
    >
    > 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?
    >[/ref]

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

    Beardy Guest

  6. #6

    Default Re: Find exact creation time of a process; not kill a wrong process

    On Fri, 21 Nov 2003 09:45:15 +0000, Beardy wrote:
     

    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

    Nils Guest

  7. #7

    Default Re: Find exact creation time of a process; not kill a wrong process

    Nils Petter Vaskinn wrote: 
    >
    >
    > 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.
    >[/ref]

    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...

    Beardy Guest

  8. #8

    Default Re: Find exact creation time of a process; not kill a wrong process

    >> (1) 
    >
    >Write the current time to a log file when you start the process.[/ref]

    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.
    --
    Michael Wang * http://www.unixlabplus.com/ * com
    Michael Guest

  9. #9

    Default Re: Find exact creation time of a process; not kill a wrong process

    In comp.unix.solaris Michael Wang <com> wrote:
     

    "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.

     
     
     

    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.

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

  10. #10

    Default Re: Find exact creation time of a process; not kill a wrong process

    In article <xGsvb.11851$news.prodigy.com>,
    Darren Dunham <taos.com> wrote: 

    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.

    --
    Michael Wang * http://www.unixlabplus.com/ * com
    Michael Guest

  11. #11

    Default Re: Find exact creation time of a process; not kill a wrong process

    [Michael Wang]: 

    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
    Kjetil Guest

  12. #12

    Default Re: Find exact creation time of a process; not kill a wrong process

    In comp.unix.solaris Kjetil Torgrim Homme <ifi.uio.no> wrote: [/ref]
     
     
     

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

    Same problem.


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

  13. #13

    Default Re: Find exact creation time of a process; not kill a wrong process

    In comp.unix.solaris Michael Wang <com> wrote: [/ref]
     

    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.

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

  14. #14

    Default Re: Find exact creation time of a process; not kill a wrong process

    In article <ifi.uio.no>,
    Kjetil Torgrim Homme <ifi.uio.no> wrote: 
    >
    >do you know the parent reliably?
    >
    > pgrep -P parent | grep "^what I think the pid is$"
    >
    >or use ps -o ppid.[/ref]

    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.
    --
    Michael Wang * http://www.unixlabplus.com/ * com
    Michael Guest

  15. #15

    Default Re: Find exact creation time of a process; not kill a wrong process

    com (Michael Wang) writes:
     

    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

    Ian Guest

  16. #16

    Default Re: Find exact creation time of a process; not kill a wrong process

    In article <home.lunanbay.com>,
    Ian Fitchet <LESS-SPAM.com> wrote: 
    >
    > 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.[/ref]

    Unlike process creation time, file creation time is known.
    stat() returns a structure which includes

    time_t st_atime; /* Time of last access */
    time_t st_mtime; /* Time of last data modification */
    time_t st_ctime; /* Time of last file status change */
    /* Times measured in seconds since */
    /* 00:00:00 UTC, Jan. 1, 1970 */

    ctime is commonly referred as "creation time". I am ok with C to
    find a process creation time, is there a way?
    --
    Michael Wang * http://www.unixlabplus.com/ * com
    Michael Guest

  17. #17

    Default Re: Find exact creation time of a process; not kill a wrong process

    com (Michael Wang) writes:
     

    Hmm, what the comment says and what you say are not the same. Given
    that you are asking the questions I would read it as that the
    comments are telling you the answers. At no point do I read the
    comment, "status change," as meaning "creation time."

    In fact, to help you a little more, here's a pointer into the FAQ for
    comp.unix.shell:

    http://www.faqs.org/faqs/unix-faq/faq/part3/section-1.html

    Cheers,

    Ian
    Ian Guest

  18. #18

    Default Re: Find exact creation time of a process; not kill a wrong process

    Ian Fitchet <LESS-SPAM.com> writes:
     
    >
    > Hmm, what the comment says and what you say are not the same. Given
    > that you are asking the questions I would read it as that the
    > comments are telling you the answers. At no point do I read the
    > comment, "status change," as meaning "creation time."[/ref]

    Along with creat -> create, perhaps this should be one change to make
    to the original unix when time machines will be available. This would
    save us so much support time to newbies...

    --
    __Pascal_Bourguignon__ . * * . * .* .
    http://www.informatimago.com/ . * . .*
    * . . /\ () . *
    Living free in Alaska or in Siberia, a . . / .\ . * .
    grizzli's life expectancy is 35 years, .*. / * \ . .
    but no more than 8 years in captivity. . /* o \ .
    http://www.theadvocates.org/ * '''||''' .
    SCO Spam-magnet: com ******************
    Pascal Guest

  19. #19

    Default Re: Find exact creation time of a process; not kill a wrong process

    Michael Wang wrote: 
    >>
    >>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.[/ref]
    >
    >
    > Unlike process creation time, file creation time is known.[/ref]

    No, it's not.
     

    Only by people who don't understand stat(). You can get yourself
    into a lot of trouble by thinking ctime is "creation time".
    For example, if I change the permissions on a file, ctime
    gets updated.

    Chris Mattern


    Chris Guest

  20. #20

    Default Re: Find exact creation time of a process; not kill a wrong process

    [Michael Wang]: 

    yes.
     

    not in Solaris. (unless you use /usr/ucb/ps, that is.)
     

    you can forget "absolute reliable programming" in Bourne shell. if
    you use a more advanced language, like Perl, Python, Ruby or even C,
    you can do your task reliably since you have almost direct access to
    the system calls.

    --
    Kjetil T. | read and make up your own mind
    | http://www.cactus48.com/truth.html
    Kjetil Guest

Page 1 of 2 12 LastLast

Similar Threads

  1. kill process when file count reached,,.
    By onlineviewer in forum PERL Modules
    Replies: 6
    Last Post: July 4th, 12:32 AM
  2. Win32::Process Kill Process in Windows ME
    By Stefan Mueller in forum PERL Modules
    Replies: 3
    Last Post: April 15th, 07:35 PM
  3. Kill remianed process
    By Khurram in forum AIX
    Replies: 3
    Last Post: December 10th, 11:14 AM
  4. process detection and kill the previous one
    By Jaguk in forum UNIX Programming
    Replies: 3
    Last Post: October 14th, 07:20 AM
  5. Kill process
    By Oleg in forum Microsoft SQL / MS SQL Server
    Replies: 4
    Last Post: July 2nd, 08:49 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139