Register
It is currently Sat Dec 20, 2014 11:33 am

Background scripts will not terminate


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Thu Oct 20, 2011 9:18 am   

Joined: Thu Oct 20, 2011 8:43 am
Posts: 2
Here is something that's frustrating me for a while. Hopefully someone can give me a hint in the right direction.

I'm trying to run a script which starts another script in the background. This background script needs to log output both to a logfile and syslog.

So basically (extreme simplification):

script1.sh:
Code:
<set parameters and variables>
LOGFILE="/var/log/results"
bash -c "script2.sh <parameters> 1> >( tee $LOGFILE.log |logger -p local7.info -t script1.sh) 2> >(  tee $LOGFILE.err |logger -p local7.err -t script1.sh)" < /dev/null &


script2.sh:
Code:
<set parameters and variable>
LOGFILE="/var/log/results"
loop=1
while [ $loop -le 10 ]
       do
       << All sorts of activities :) >>
       ((loop ++))
done


My problem is that script2.sh spawns child processes for the tee/logger fucntion which don't allways terminate after the parent proces exits.

So during a run, my process tree look like:
Code:
24976 ?        S      0:00 bash -c script2.sh <parameters> 1> >( tee /var/log/results.log |logger -p local7.info -t script1.sh) 2> >(  tee /var/log/results.err |logger -p local7.err -t script1.sh)
24978 ?        S      0:00  \_ bash -c script2.sh <parameters>
24979 ?        S      0:00      \_ bash -c script2.sh <parameters> 1> >( tee /var/log/results.log |logger -p local7.info -t script1.sh) 2> >(  tee /var/log/results.log |logger -p local7.err -t script1.sh)
24980 ?        S      0:00      |   \_ tee /var/log/results.log
24981 ?        S      0:00      |   \_ logger -p local7.info -t script1.sh
24982 ?        S      0:00      \_ bash -c script2.sh <parameters> 1> >( tee /var/log/results.log |logger -p local7.info -t script1.sh) 2> >(  tee /var/log/results.log |logger -p local7.err -t script1.sh)
24983 ?        S      0:00          \_ tee /var/log/results.err
24984 ?        S      0:00          \_ logger -p local7.err -t script1.sh


script1.sh, which originally is the parent of script2.sh, is terminated as soon as script2.sh is started in the background. Therefore, script2.sh runs from init

After script2.sh terminates (PID 24976 in this example), not all children exit. At least one of the logging processes stays active:

Code:
24979 ?        S      0:00      \_ bash -c script2.sh <parameters> 1> >( tee /var/log/results.log |logger -p local7.info -t script1.sh) 2> >(  tee /var/log/results.log |logger -p local7.err -t script1.sh)
24980 ?        S      0:00          \_ tee /var/log/results.log
24981 ?        S      0:00          \_ logger -p local7.info -t script1.sh


This is not what I want :) I would want ALL processes associated with the parent to exit. Is there a way to force this?

Running the command for script2.sh from shell works fine, but then the script runs under the PID of my terminal session...


Top
 Profile  
 PostPosted: Fri Oct 21, 2011 1:28 am   

Joined: Thu Oct 20, 2011 8:43 am
Posts: 2
Additional: It looks like the tee process is the culprit. Once I kill that process the other two disappear as well... Still no clue as to why this particular instance of tee won't quit...


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP