Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created July 15, 2012 20:55
Show Gist options
  • Select an option

  • Save cowboy/3118588 to your computer and use it in GitHub Desktop.

Select an option

Save cowboy/3118588 to your computer and use it in GitHub Desktop.

Revisions

  1. cowboy revised this gist Jul 16, 2012. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions sudo-keepalive-example.sh
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,12 @@
    #!/bin/bash

    # Ask for password.
    sudo true
    # Might as well ask for password up-front, right?
    sudo -v

    # Update existing sudo time stamp if set, otherwise do nothing.
    # Keep-alive: update existing sudo time stamp if set, otherwise do nothing.
    while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &

    # Do tons of stuff over the next 30+ mins that requires sudo here or there.
    # Example: do stuff over the next 30+ mins that requires sudo here or there.
    function wait() {
    echo -n "["; for i in {1..60}; do sleep $1; echo -n =; done; echo "]"
    }
  2. cowboy revised this gist Jul 15, 2012. 1 changed file with 26 additions and 5 deletions.
    31 changes: 26 additions & 5 deletions sudo-keepalive-example.sh
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,31 @@
    #!/bin/bash

    # ask for password
    # Ask for password.
    sudo true

    # keep sudo from timing out
    while true; do sudo -v; sleep 60; kill -0 "$$" 2>/dev/null || exit; done &
    # Update existing sudo time stamp if set, otherwise do nothing.
    while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &

    # do tons of stuff over the next 60 mins that requires sudo only here or there
    # ...
    # Do tons of stuff over the next 30+ mins that requires sudo here or there.
    function wait() {
    echo -n "["; for i in {1..60}; do sleep $1; echo -n =; done; echo "]"
    }
    wait 0 # show reference bar
    echo "$(sudo whoami) | $(date)"
    wait 1
    echo "$(sudo whoami) | $(date)"
    wait 2
    echo "$(sudo whoami) | $(date)"
    wait 5
    echo "$(sudo whoami) | $(date)"
    wait 10
    echo "$(sudo whoami) | $(date)"
    wait 15
    echo "$(sudo whoami) | $(date)"
    wait 1
    sudo -K
    echo "$(whoami) | $(date)"
    wait 2
    echo "$(whoami) | $(date)"
    wait 5
    echo "done."
  3. cowboy revised this gist Jul 15, 2012. 1 changed file with 2 additions and 9 deletions.
    11 changes: 2 additions & 9 deletions sudo-keepalive-example.sh
    Original file line number Diff line number Diff line change
    @@ -4,14 +4,7 @@
    sudo true

    # keep sudo from timing out
    pid=$$
    {
    while true; do
    sudo -v
    sleep 60
    kill -0 "$pid" 2>/dev/null || exit
    done
    } &
    while true; do sudo -v; sleep 60; kill -0 "$$" 2>/dev/null || exit; done &

    # do tons of stuff over the next 60 mins that might require sudo here or there
    # do tons of stuff over the next 60 mins that requires sudo only here or there
    # ...
  4. cowboy created this gist Jul 15, 2012.
    17 changes: 17 additions & 0 deletions sudo-keepalive-example.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    #!/bin/bash

    # ask for password
    sudo true

    # keep sudo from timing out
    pid=$$
    {
    while true; do
    sudo -v
    sleep 60
    kill -0 "$pid" 2>/dev/null || exit
    done
    } &

    # do tons of stuff over the next 60 mins that might require sudo here or there
    # ...