You might want to use -w
with killall
for that:
-w, --wait
Wait for all killed processes to die. killall checks
once per second if any of the killed processes still exist
and only returns if none are left. Note that killall may
wait forever if the signal was ignored, had no effect,
or if the process stays in zombie state.
So killall -w rogue_agents
would be enough.
Edit: For the additional bonus question in your edit...
(about upgrading it to a kill -9
after some specified period of time)
You could try the following:
(i can't try this myself at the moment because i have no rogue processes :)
timeout 8 killall -w rogue_agents
timeout 8 killall -9 -w rogue_agents
This will do a killall
with a wait. The timeout 8
makes it wait only 8 seconds after which you can do the killall -9
. I also made that one wait 8 seconds. If the killall -9
isn't done after 8 seconds there is really something wrong and it would need manual investigation.
You could of course build in a test to only execute the second line if there are still rogue_agents present after the initial killall
after 8 seconds. And an echo with an error if the process is still active after the second killall -9
.