I need to tag a generic process (I don't really control the command being run).

Could I do something like this:

   "$@"  -- --very-special-command

run_special_cmd sleep 5000 && echo 'bar'

and then later use:

pkill -9 -f '--very-special-command'

to id those processes to be killed? Or is there a better generic way to tag procs for later identification?

  • Put something in its environment. Or use special_command& special_pid=$!; ...; kill $special_pid. – Mikel Sep 25 '18 at 5:54

Not all command would ignore arbitrary arguments that you pass them.

If the called process doesn't fork, you can save the pid of the calling process and call it with exec. Use a script

echo $$ > /var/run/foo.pid
exec "$@"

This will execute the command in the same process, with the pid saved before the process was executed.

If you want the pid file removed, you can delete it after the process exits.

"$@" &
echo $! > $PIDFILE
rm -f $PIDFILE

This will start the process in the background, record the pid, wait for the process to exit and then remove the file.

  • yeah i really need to tag processes by name instead of pid, otherwise pids could be stale or the wrong pid, since pids can be reused. – Alexander Mills Sep 25 '18 at 5:58
  • A added a way to remove the file after the process exits. – RalfFriedl Sep 25 '18 at 6:33

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.