Job control: How to kill a sudo job using the job id
If I run jobs with sudo
I can't kill %1
them (Operation not permitted). My first thought was to use sudo kill %1
instead, but that of course doesn't work either because it won't use the bash builtin kill
. Is there a trick to make this work?
// I know how to kill a process through it's PID. The question is specifically about how to use the job id to kill a sudo job.
sudo kill "$(jobs -p %1)"
NOTE: Technically the double quotes aren't necessary, here, because we know the output of jobs
will be a PID, and so won't contain any characters in $IFS
. However, quoting variables is always a good habit to be in. And to ensure you use quality coding practices consistently, they must be ingrained habits. $(...)
is used over backticks because backticks are the old way of doing things and they're less visible. $(...)
is the new hotness, relatively speaking. The new hotness is, in this case, a few decades old.