I want only one instance of my shell script to run at once, with new ones killing old ones.
I'm on Linux, and thought this was a good approach:
# Terminate other instances by name and name self.
kill -15 $(printf -- '-%s ' $(pgrep mychosenname))
echo mychosenname >| /proc/$$/comm
Note the printf to negate the PIDs because I want kill the child processes too. It seemed to work perfectly. But it doesn't work for a process spawned from my ~/.xprofile (when X starts) for some reason.
ps -a
shows:
...
924 tty1 00:00:00 mychosenname?
...
pgrep -a mychosenname
shows:
924 sh /home/sedwho/projects/mychosenname/run
Manually trying kill -15 -924
I get:
kill: kill -924 failed: no such process
And ps
and pgrep
give the same output as before.
kill -15 924
kills it but not the child processes :(