Given the full file name of a process, how can I kill it? Not only by its file name, but by full file name. I've looked into kill and pkill and they're not what I'm looking for.
Asked
Active
Viewed 4,020 times
0
-
What operating system are you using? – Jenny D May 18 '18 at 16:04
-
I used pkill and it works, pkill -9 /usr/sbin/td-agent – c4f4t0r May 18 '18 at 17:36
2 Answers
2
If your script can be shown by its full name including the path using ps, you can easily do that:
MYPID=$( ps faux | grep '/tmp/test.sh' | grep -vw grep | awk '{ print $2 }' );
echo ${MYPID};
kill -9 ${MYPID};
Note: I run it on Debian Jessie, so if you do so or use a Debian-based distro, it should work.

Tomasz Pala
- 408
- 2
- 6

Craft
- 171
- 1
- 5
0
On Linux with lsof you could do this and pipe the output to kill
.
I use lsof
to search for processes holding the executable, and then check whether the executable of that process points to the file of interest.
NAME=/my/object/that/executes
CANON=`readlink -en "$NAME"`
[ -n "$CANON" ] || exit 1
for pid in `lsof -tf -- "$CANON"`
do
PIDEXE=`readlink -en /proc/$pid/exe`
[ x"$CANON" == x"$PIDEXE" ] && echo $pid
done
Limitations:
This requires the executable to be still visible in the file system and you will not find processes that started with this executable in a different location or that only used the executable for bootstrapping.
And beware, this sort of thing is very sensitive to race conditions, you might end up killing innocent processes.

Gerrit
- 1,552
- 8
- 8