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