-1

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.

Korim
  • 17
  • 1
  • 3

2 Answers2

1

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
  • 398
  • 1
  • 6
Craft
  • 161
  • 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,347
  • 7
  • 8