mediatribe.net -- Drupal et développement web

Notice: this post was last updated Il y a 3 years 35 weeks so it might be outdated. Please be cautious before implementing any of suggestions herein.

Exit parent process with the code of the child process on Mac OS X

Sometimes you want the parent process to exit with the child process's code. In the example below, whatever is in parentheses is child code. For example, calling exit 1 directly exists the process, but calling (exit 1) does not exit the process.

(exit 1)
#parent still running
(exit 0)
#parent still running
(exit 1)
#parent still running
[ $? = 0 ] || exit $?
# exit parent with exit code of child process if non-zero.

$? corresponds to the last exit code of the child process. What we are doing is checking if it is equal to zero, and if it is not, continue by exiting with that exit code.

Mac OS X, CentOS and other systems seem to behave differently with regards to exit codes when using Jenkins: on CentOS, a Jenkins Job will fail if any of the commands fail. On Mac OS X, if you don't add the line [ $? = 0 ] || exit $? to the shell script, the job never seems to fail.