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.
2 Answers
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.
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.