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 :(

1 Answer 1


It is not a negative process id.

A negative integer passed as the 2nd argument to kill represents a process group id (pgid).

To answer your question, you can not send the signal to the process group id, because there is not process group id with that number.

These may be useful: ps -ao pid,pgid,user,time,command ps -q 924 pgid=

How is a process group ID set?

ps man page

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .