UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old November 12th, 2004, 08:28 AM
dev0173 dev0173 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Location: UK
Posts: 18 dev0173 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #2  
Old November 12th, 2004, 10:47 AM
Perderabo Perderabo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 121 Perderabo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 54 sec
Reputation Power: 5
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.

Reply With Quote
  #3  
Old November 12th, 2004, 11:46 AM
dev0173 dev0173 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Location: UK
Posts: 18 dev0173 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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:

Quote:
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

Reply With Quote
  #4  
Old November 12th, 2004, 12:01 PM
Perderabo Perderabo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 121 Perderabo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 54 sec
Reputation Power: 5
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.

Reply With Quote
  #5  
Old November 15th, 2004, 04:51 AM
dev0173 dev0173 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Location: UK
Posts: 18 dev0173 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Orphaned sleep processes


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

IBM developerWorks




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway