Why does the script keep running when the background processes are done?

Why does the script keep running when the background processes are done?

Now, our script will stay running until the background processes finish, but if we ctrl+c, our background processes will still stay running. We can fix that with a trap. This will cause the command kill 0 to run when our script exits.

How to kill background processes / jobs when my shell script exits?

Another option is it to have the script set itself as the process group leader, and trap a killpg on your process group on exit. jobs -p does not work in all shells if called in a sub-shell, possibly unless its output is redirected into a file but not a pipe.

How to start and stop background processes in Bash?

A Script to Start (and Stop!) Background Processes in Bash Simple Way to Start (and Stop!) Background Processes in Bash I’ve often wanted a way to easily start and stop a group of processes from the command line.

Which is the correct way to terminate a background process?

The correct way is to type jobs then use the job number to kill it. In order to use the pid to kill it you need to bring it to the foreground as noted in the first answer. One thing I don’t see here, which I’ve found very useful especially when testing out commands, is pidof.

How to clean up background processes in Bash?

This final script should clean up background processes in all the following cases: 1 We ctrl+c while running the script. 2 We kill the script in some other way externally (e.g. with kill or killall ). 3 The script encounters an error. More

How to kill a process in bash script?

We can fix that with a trap. trap “kill 0” EXIT ./someProcessA & ./someProcessB & wait. This will cause the command kill 0 to run when our script exits. The kill 0 will kill our process, and all of our child processes.

Is there an alternative to running the whole script in background?

An alternative would be running the whole script in background. ‘Would be’, but the read command is unable get the user input if the script is run as startserv.sh &.