Question

I have a 4 or 5 commands being run in separate processes that i would like to be able to kill together via a script.

They are make commands, and I could just run killall make or pkill make but that kills other make commands I may have running. So I'm thinking there may be a way I could open these processes as part of a group of processes, and then I could kill them as a group or loop over items in the group and kill them that way.

One way I have is to switch to a different user to open the processes and then when I kill them just kill that users session, but if there's a way to just group these processes, or hold onto there ids some how that would be great.

Any ideas?

Let me know if more info is needed but what im hoping for is something like:

openwithgroup groupname 'make command1'
openwithgroup groupname 'make command2'
openwithgroup groupname 'make command3'

# and then to kill them, maybe something like:
killgroup groupname
Was it helpful?

Solution

You could store all their PIDs in an array and kill those:

pids=()
make command1 &
pids+=($!)
make command2 &
pids+=($!)
make command3 &
pids+=($!)

kill ${pids[@]}

If you are running all of these commands in the same script, then AFAIK the PIDs will not be reused, and you can be sure that you're killing the correct processes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top