Not bad, but one big criticism, never do a 'kill -9' first, that will stop the program from cleaning up after itself if killed using -9.
Use one of these instead:
-TERM then wait, if not
-INT then wait, if not
-HUP then wait, if not
-ABRT
If you are sure all of these fail, then use -9 (-KILL). But assume the program has a major bug and try and find another program that will do the same task and use that instead.HUP is usually sent to daemons to instruct them to reinitialize and reread their configuration files.
Is it still passed when a terminal is disconnected? I understand a dial-up modem was involved in the original intended use.
How often does plain 'kill <pid>' not work, but some other signal other than SIGKILL works?
Usually the process is either working correctly and terminates when asked, or else not working correctly and needs to be KILLed.
Never use `kill -9`, instead refer to the signal directly. 9 is not always the same signal on all platforms.
This is article is likely LLM generated and it regurgitates as first go what the last resort should be. After seeing that command I stopped reading.
Maybe this logic should be built into the "kill" command (or some other standard command). Given that this is the right way, it shouldn't be more tedious than the wrong way!
It could also monitor the target process and inform you immediately when it exits, saving you the trouble of using "ps" to confirm that the target is actually gone.