-
-
Save antoni/10d75b7822c1b839a6b9c5deb4705d8c to your computer and use it in GitHub Desktop.
Bash: Sudo keep-alive (good for long-running scripts that need sudo internally but shouldn't be run with sudo)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Might as well ask for password up-front, right? | |
sudo -v | |
# 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 & | |
# 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 "]" | |
} | |
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." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment