Search code examples
zshsudo

running multiple sudo'd commands concurrently


I've once again been bitten by out-of-date caches. I finally got tired of looking up all the cache rebuilding commands and decided to write myself a short zsh function to do all the ones I know about (and as I discover more they can go in here). Here's what I've got at the moment:

recache() {
    sudo true
    sudo updatedb &
    local updatedbpid=$!
    sudo ldconfig &
    local ldconfigpid=$!
    rehash
    wait $updatedbpid $ldconfigpid
}

The first sudo true is intended to make sure sudo has a recent timestamp, so that the following background processes don't sit suspended on input waiting for me to type my password. I don't use sudo -b because I want to wait until the caches are up to date before continuing my work.

Most of the time, this works great; but about one time in three or so, I get endless copies of the following text:

[2]  + suspended (tty output)  sudo updatedb

Why? What can I do to fix it?


Solution

  • So I never did figure out why this was happening. My best guess is there's some resource that sudo is trying to lock, and the two invocations of sudo get in a race for it. In any case, I've been using the following modified recache function for some time without trouble. The main difference is that there is only a single backgrounded sudo, and it runs both updatedb and ldconfig in parallel, rather than one backgrounded sudo per process.

    recache() {
        sudo true
        sudo zsh -c 'updatedb & ldconfig & wait' &
        local pid=$!
        rehash
        wait $pid
    }