Terminate Background Child Process in Bash
In Bash by child process that sent into background is still alive when main program is terminated. Take a look an example below.
#!/bin/bash
# Run a Python web server
python -m http.server src/ &
# Run SASS watcher and compiler...
sass --watch src/scss:src/css &
waitSolution to terminate Background Child Process in Bash
The solution to above problem is using shell built-in trap command.
#!/bin/bash
# Kill all child process (Python and SASS) when exit
trap "kill 0" EXIT
# Run a Python web server
python -m http.server src/ &
# Run SASS watcher and compiler...
sass --watch src/scss:src/css &
waitNow when the script is exited all the child process even which has been sent as background process will also terminated.
 
0 comments:
Post a Comment