I have shell script test.sh
that I running.
I want that in the beginning of this script ,if there are older test.sh
that running ,kill them first.
If I killall test.sh
the newest process killed too.
I there any simple way to do it ?
killshell-script
I have shell script test.sh
that I running.
I want that in the beginning of this script ,if there are older test.sh
that running ,kill them first.
If I killall test.sh
the newest process killed too.
I there any simple way to do it ?
Best Answer
This script (of which there is a simpler version below) installs a signal handler, the
term_handler
function, which will exit the script upon receiving aTERM
signal if theok_to_exit
variable's value is1
.In the body of the script is a loop simulating some form of work being carried out. The important bit is that before calling
pkill
(you may change this to yourkillall
command), it setsok_to_exit
to0
, and directly afterwards it sets it to1
.When sending out the
TERM
signal to all matching processes, it will receive the signal itself, but it will refuse to exit. Any other matching processes, if it's not also in the exact same state (which could happen if you started the script more than once simultaneously), would terminate.When running, this script would output
When another copy of the script is started, it outputs
exiting
and then terminates.A simpler version of the same script:
This simply ignores the
TERM
signal during the call topkill
, and then reinstates the default signal handler before continuing.