#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Location
    UK
    Posts
    18
    Rep Power
    0

    Orphaned sleep processes


    Hi All,

    I'm running a ksh script that runs a background process that spends most of its time in the sleep command. When I list the processes I can see my main script, with the background script listed as a child process, and the sleep listed as a child of that.

    When my main script kills the background script it leaves the sleep process running but sets it parent process ID to 1, effectively orphaning the sleep.

    Does anyone know of a way of making sure that the sleep is killed when its parent is killed?

    Thanks

    Steve
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    121
    Rep Power
    16
    Those sleep processes become owned by init and init will clean them up when they exit.

    But here is something to try. In the main script turn on job control:
    set -o monitor

    Capture the pid of the background process.
    /path/to/other_script &
    pid=$!

    When you want to kill the background script together with those sleep processes use the syntax:
    kill -15 -${pid}

    For this to work, that background script must not also turn on monitor mode. And your OS must support posix style process groups.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Location
    UK
    Posts
    18
    Rep Power
    0
    Hmm ... can't seem to get that to work.

    The kill reports "The specified process does not exist".

    From looking at the man page, it seems that it's expecting a negative process ID to actually be a process group ID:

    If it [the process number] is a negative number but not -1, the kill command sends the signal to all processes that have a process group ID equal to the absolute value of the PID.
    Never mind. Those sleep processes aren't doing any harm other than taking up an entry in the process table. I'll leave them alone until inspiration strikes.

    Thanks anyway.

    Steve
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    121
    Rep Power
    16
    That's right, you need to hit a process group. The process group is is the pid of the process group leader. And this doesn't change even if the process group leader exits. I just tried this with a simple script called sleeper:

    #! /usr/bin/ksh
    sleep 60 &
    sleep 60 &
    sleep 60 &
    sleep 15
    exit 0

    And this script:
    #! /usr/bin/ksh
    set -o monitor
    ./sleeper &
    pid=$!
    echo pid = $pid
    kill -15 -${pid}
    ps -f
    exit 0

    And it works for me on HP-UX and SunOS.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Location
    UK
    Posts
    18
    Rep Power
    0
    When I tried that script on my AIX 5.2 box ... it worked perfectly as well. And the technique worked when I re-applied it to my larger script! I must have fouled something up on the first attempt. Fat fingers causing trouble I guess.

    Many thanks.

    Steve

IMN logo majestic logo threadwatch logo seochat tools logo