Skip to content

Instantly share code, notes, and snippets.

@lucabertolasi
Last active December 23, 2018 13:52
Show Gist options
  • Save lucabertolasi/f8cf7021f26b687910d239a6ede031da to your computer and use it in GitHub Desktop.
Save lucabertolasi/f8cf7021f26b687910d239a6ede031da to your computer and use it in GitHub Desktop.
[Bash] How to exit the parent script if a child die? Call "./child.sh || exit 1" from the parent. Here an example:
#!/bin/bash
## LICENSE
## Copyright (c) 2017, Luca Bertolasi. All Rights Reserved.
##
## This source code is licensed under the terms of the MIT license,
## a copy of which can be found at https://choosealicense.com/licenses/mit/
cat << EOF > $(pwd)/parent.sh
#!/bin/bash
echo parent...start child_success
echo
./child_success.sh || exit 1 ## this line will succeed and not affect execution
echo
echo parent...start child_failure
echo
./child_failure.sh || exit 1 ## this line will fail and make the parent exit immediately
echo
echo parent...end
EOF
cat << EOF > $(pwd)/child_success.sh
#!/bin/bash
echo child_success...start
echo child_success...success
echo child_success...end
EOF
cat << EOF > $(pwd)/child_failure.sh
#!/bin/bash
echo child_failure...start
echo child_failure...failure
exit 1 ## here we are simulating a command that will fail and exit with code 1, making the parent exit immediately
echo child_failure...end
EOF
chmod u+x $(pwd)/parent.sh $(pwd)/child_success.sh $(pwd)/child_failure.sh
./parent.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment