Kill process by pid file

cat /var/run/myProcess.pid | sudo xargs kill -9

In some situations, the more compact:

pkill -F /var/run/myProcess.pid

is the way to go. I've had trouble with the varieties:

kill $(cat /var/run/myProcess.pid)
# Or
kill `cat /var/run/myProcess.pid`

when I had to put the command into something else which might parse it using different rules, like Monit does for its start/stop commands.


I believe you are experiencing this because your default shell is dash (the debian almquist shell), but you are using bash syntax. You can specify bash in the shebang line with something like,

#!/usr/bin/env bash

Or, you could use the dash and bash compatible back-tick expression suggested by admdrew in the comments

kill -9 `cat /var/run/myProcess.pid`

Regardless, you can't rely on /bin/sh to be bash.

Tags:

Linux