10

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.

5
  • man kill, man ps, man pgrep, man pkill, help jobs Commented Mar 16, 2016 at 18:32
  • 1
    Only the last one has anything to do with what I'm trying to solve and it doesn't have a solution. Any more specific hints? Commented Mar 16, 2016 at 18:36
  • 1
    I don't understand what the problem is here, but you can explicitly use the builtin instead of a command from your PATH through usage of builtin, e.g. builtin kill. Commented Mar 17, 2016 at 23:00
  • @XiongChiamiov You can't use builtins with sudo. sudo builtin kill -l gives sudo: builtin: command not found
    – wisbucky
    Commented Mar 7, 2019 at 0:03
  • "sudo won't use the bash builtin kill". Aside from that, sudo won't see the same list of jobs. If you do sudo jobs, you won't see %1 listed. So it will never work with %1. You have to use the actual PID (which is effectively what Parthian's answer is doing).
    – wisbucky
    Commented Mar 7, 2019 at 0:14

1 Answer 1

7
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.

4
  • 2
    I was hoping that there is an elegant solution to that problem. Obviously that one is not elegant. What's wrong with trying to find an elegant solution for that? I thought there must be something built in for that, but it seems like I have to define an alias for that. Commented Mar 16, 2016 at 19:07
  • @S1lentSt0rm This is a one-liner. Doesn't get much better than that. My main beef with this question is that SF != code golf SE. If you know the canonical way to solve the problem is killing via PID, and that question is already answered, and the question of how to convert PID to JID is trivially answered by the documentation, this question adds data to the site without adding value. Granted, I'm part of the problem because I rose to the bait and answered the question. And I hate myself for it. Commented Mar 16, 2016 at 19:10
  • A simpler one liner is sudo kill `jobs -p %1`
    – user
    Commented Dec 21, 2020 at 20:33
  • @user I'd recommend developing the habit of never using backticks. They're the older method of command substitution, and were replaced by parens precisely because they handle nesting very poorly, and lead to readability issues. See: unix.stackexchange.com/a/5782/74972 Commented Jan 4, 2021 at 4:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .