Skip to content
Advertisement

Bash trap command failing to catch fatal php script exit code 255

Minimal example of the issue:

#!/bin/bash

errHandler() {
    echo "Something went wrong. Exiting now."
    exit 1
}
trap 'errHandler' ERR INT

pi_process () {
    bin/pi $@
    echo $?
}

pi_process stuff

bin/pi is a php script, it’s exiting with exit code 255 and a fatal error. However the bash trap isn’t being caught. How come?

Advertisement

Answer

set -o functrace does not seem to do what you expected.

Try this :

#! /bin/bash

set -e

errHandler() {
    echo "Something went wrong. Exiting now."
    exit 1
}
trap 'errHandler' EXIT

pi_process () {
    false
    echo $?
}

pi_process stuff
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement