Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kill process by pid file

Tags:

linux

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.

like image 605
UpCat Avatar asked Sep 29 '14 19:09

UpCat


1 Answers

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.

like image 196
Elliott Frisch Avatar answered Oct 07 '22 21:10

Elliott Frisch