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.

  • 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

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.

CANON=`readlink -en "$NAME"`
[ -n "$CANON" ] || exit 1
for pid in `lsof -tf -- "$CANON"`
 PIDEXE=`readlink -en /proc/$pid/exe`
 [ x"$CANON" == x"$PIDEXE" ] && echo $pid


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.