Search code examples
linuxbashpkill

Pass bash command a comment to see from pkill


I have an aribtrary bash command being run that I want to attach some identifying comment to so that I may pkill it if necessary.

For example:

sleep 1000 #uniqueHash93581
pkill -f '#uniqueHash93581'

... but the #uniqueHash93581 does not get interpreted, so pkill won't find the process.

Any way to pass this unique hash so that I may pkill the process?


Solution

  • Bash removes comments before running commands.


    A workaround with Linux and GNU grep:

    Prefix your command with a variable with a unique value

    ID=uniqueHash93581 sleep 1000
    

    Later search this variable to get the PID and kill the process

    grep -sa ID=uniqueHash93581 /proc/*/environ | cut -d '/' -f 3 | xargs kill