Skip to content

Instantly share code, notes, and snippets.

@antoni
Forked from cowboy/sudo-keepalive-example.sh
Created January 6, 2019 13:14
Show Gist options
  • Save antoni/10d75b7822c1b839a6b9c5deb4705d8c to your computer and use it in GitHub Desktop.
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)
#!/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
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment