0

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?

cars
  • 421
  • 7
  • 18

2 Answers2

1

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
Cyrus
  • 84,225
  • 14
  • 89
  • 153
1

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"
chepner
  • 497,756
  • 71
  • 530
  • 681