63

I try to kill a process by pid file:

kill -9 $(cat /var/run/myProcess.pid)

The pid file contains the process number. However executing the kill gives me no stdout and the processes is still alive. But this works:

kill -9 PID

What is wrong with the first kill command? Does it fail to extract the PID from the file?

Example content of pid file:

5424

and

kill -9 5424

works.

UpCat
  • 67,448
  • 127
  • 369
  • 590
  • 3
    I'm guessing, but try `cat /var/run/myProcess.pid | xargs kill -9`; my assumption is that you have trailing white-space in the pid file. – Elliott Frisch Sep 29 '14 at 19:11
  • @ElliottFrisch nope is not working – UpCat Sep 29 '14 at 19:12
  • Post the contents of the pid file. – Elliott Frisch Sep 29 '14 at 19:13
  • @ElliottFrisch just a number, see my updated question – UpCat Sep 29 '14 at 19:13
  • 1
    @ElliottFrisch yes it prints 5424. – UpCat Sep 29 '14 at 19:16
  • 4
    @artworkadシ Does ``kill -9 `cat /var/run/myProcess.pid` `` work? (had to figure out how to print backticks inside of backticks, heh) – admdrew Sep 29 '14 at 19:16
  • Is this in a script, or at the command line? If it's a script what is your shebang line and are you on Ubuntu? – Elliott Frisch Sep 29 '14 at 19:20
  • 1
    @admdrew this works, can you explain where the difference is? – UpCat Sep 29 '14 at 19:30
  • 1
    @ElliottFrisch I am on debian, and the command is called from a nodejs script. The problem was solved with kill -9 `cat /var/run/myProcess.pid` – UpCat Sep 29 '14 at 19:30
  • 2
    @artworkadシ Is your `/bin/sh` bash, or is it dash? – Elliott Frisch Sep 29 '14 at 19:31
  • 1
    @artworkadシ I actually didn't know; looks like they're equivalent, but `$()` is the newer [POSIX standard](http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_03) ([source1](http://stackoverflow.com/questions/9405478/command-substitution-backticks-or-dollar-sign-paren-enclosed), [source2](http://stackoverflow.com/questions/4708549/shell-programming-whats-the-difference-between-command-and-command)) – admdrew Sep 29 '14 at 19:32
  • Be aware most of the times `-9` is not required and not recommended. Give process a chance to properly cleanup its resources. – Umur Gedik Feb 03 '19 at 04:46

4 Answers4

85

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.

Community
  • 1
  • 1
Elliott Frisch
  • 191,680
  • 20
  • 149
  • 239
75

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.

jeteon
  • 3,141
  • 25
  • 39
3
cat /var/run/myProcess.pid | sudo xargs kill -9
ehsan houshmand
  • 333
  • 3
  • 6
  • 1
    While this may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – leopal Jan 23 '20 at 07:13
0

There is very simple method.

fuser -k /path/filename

example lets say you wanna kill apt lock file in linux.

sudo fuser -k /var/lib/dpkg/lock

and it will kill the process that holds the file.

Ry Van
  • 181
  • 1
  • 9