I've seen suggestions how I can terminate my script if another instance of it is already running. My problem is the other way around: I would like to find a way to just kill all other instances of the same script -- if any -- and continue with executing my script. Is there any way to do that?
You can do something like...
PIDS=$(pidof -x nameofyourscript) to get all the pids, if any, including scripts. Then you can just
EDIT: As the above would kill yout current script too, then revise the answer, adding option
-o like this:
PIDS=$(pidof -x -o $$ nameofyourscript)
-o $$ would make it omit the pid of your current script.
And better yet, instead of hardcoding nameofyourscript, just get its own name from
kill $(pidof -x -o $$ $0)
So put that near the beginning of your script, maybe with a
2>/dev/null, in case it finds no other instances, and problem solved.