The Daily Build

Icon

Software Development, version 3.0

Hassle Free Way to Kill Sudo’d Jobs

Every now and then I have to run a foreground job under sudo that doesn’t want to die when I hit ^C. Then it’s a hassle to ^Z, get the pid of the sudo job, and sudo kill that pid.

So I wrote a little script (or a template for scripts) that runs the sudo job in the background (but preserves stdout/stderr) and relies on bash to clean up the job when you ^C the script. Only gotcha with this is that you may have to retype your sudo password when you ^C if your authentication has timed out by the time you get around to killing it.

#!/bin/bash

function cleanup()
{
    sudo kill $job_pid
    wait $job_pid
    exit 0
}

trap cleanup SIGTERM
trap cleanup SIGINT

sudo long_running_foreground_process &
job_pid=$!
wait
Share and Enjoy:
  • del.icio.us
  • Digg
  • Sphinn
  • Facebook
  • Mixx
  • Google Bookmarks
  • Twitter
  • FriendFeed
  • Posterous
  • email

Like this post? Get updates from my feed.

Related posts:

  1. Using Python’s ctypes to Call Into C Libraries The ctypes module makes loading and calling into a dynamic...
  2. Python Exception Handling: Cleanup and Reraise I’ve had this code around for a while and...
  3. Makefiles are Software Too This post was inspired by recent experience with some...
  4. Moving to zsh To get started: sudo aptitude install zsh chsh /bin/zsh...
  5. Set Your zsh Prompt Since the beginning of time, all the cool kids have...

Category: news, tools

Tagged: , , ,

One Response

  1. William L. Dye ("willdye") says:

    I’m embarrassed that I didn’t think of this approach before. I’ve been using a system which was much more complicated, simply because I didn’t think things through more carefully. Thanks for sharing.

Leave a Reply

Related Posts

Related posts:

  1. Using Python’s ctypes to Call Into C Libraries The ctypes module makes loading and calling into a dynamic...
  2. Python Exception Handling: Cleanup and Reraise I’ve had this code around for a while and...
  3. Makefiles are Software Too This post was inspired by recent experience with some...
  4. Moving to zsh To get started: sudo aptitude install zsh chsh /bin/zsh...
  5. Set Your zsh Prompt Since the beginning of time, all the cool kids have...