Linux – Unbuffer swallows the exit status of killed process

bashexpectlinux

The expect "unbuffer" command seems to swallow the exit status of its child if the process is killed. Is there something that can be done to stop this behavior? It does not swallow the exit status if the child process exits with a non-zero exit status in general.

To witness this behavior, consider 2 bash scripts:

a.sh:

    exit 1

vs

b.sh:
    kill -11 $$

then do unbuffer on each of them and print $?. In the first case I see 1 but in the second case I see 0 when I expect 139

Best Answer

Yes, you have to modify the "unbuffer" script to change the way "unbuffer" wait for the child process to terminate.

From expect documentation:

Additional elements may appear at the end of the return value from wait. An optional fifth element identifies a class of information. Currently, the only possible value for this element is CHILDKILLED in which case the next two values are the C-style signal name and a short textual description.

In "unbuffer" script, look for exit [lindex [wait] 3] and replace it by the following code:

set result [wait]
send_user "wait returned: $result\n"
if { [llength $result] == 4 } {
    exit [lindex $result 3]
} else {
    exit 1
}

After that modification you will clearly see the difference. For example, running your b.sh script you will get something like this:

wait returned: 8606 exp6 0 0 CHILDKILLED SIGSEGV {segmentation violation}

You may of cource change the value of exit 1 by any value you wish. You can exit with 139 if you wish, but the value won't change to match the signal unless you add even more code to the "unbuffer" script.