I am looking for a way to clean up the mess when my top-level script exits.
Especially if I want to use set -e, I wish the background process would die when the script exits.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
To clean up some mess,
trapcan be used. It can provide a list of stuff executed when a specific signal arrives:but can also be used to execute something if the shell exits:
It’s a builtin, so
help trapwill give you information (works with bash). If you only want to kill background jobs, you can doWatch out to use single
', to prevent the shell from substituting the$()immediately.