Thursday, 7 June 2012

Bash, and process termination reports

Bash is not supposed to report on process termination unless job control is turned on - and it isn't when running scripts.

Yet it will report on process termination if a background process which is not the last background process is terminated uncleanly:


This wrongly gives a terminated report:
bash -c 'trap "sleep 1" EXIT ; ( sleep 3 & wait $! ; kill -15 -$$ ) & X=$( sleep 4 )'


This fixes it by using setsid:
bash -c 'trap "sleep 1" EXIT ; ( sleep 3 & wait $! ; exec setsid bash -c "kill -15 -$$" ) & X=$( sleep 4 )'

This breaks it by using &&:
bash -c 'trap "sleep 1" EXIT ; : && ( sleep 3 & wait $! ; exec setsid bash -c "kill -15 -$$" ) & X=$( sleep 4 )'

The answer is to use if/then/fi and not &&:
bash -c 'trap "sleep 1" EXIT ; if : ; then ( sleep 3 & wait $! ; exec setsid bash -c "kill -15 -$$" ) & fi ; X=$( sleep 4 )'



No comments:

Post a Comment