Pass bash command a comment to see from pkill

194 views Asked by At

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?

2

There are 2 answers

0
Cyrus On BEST ANSWER

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
0
chepner On

exec the command in a subshell, and use the -a option to give it a recognizable name. For example:

$ (exec -a foobar sleep 1000) &
$ ps | grep foobar
893 ttys000    0:00.00 foobar 10

Or, just run the job in the background and save its PID.

$ sleep 1000 & pid=$!
$ kill "$pid"