Let's say, I have the following bash script:
#!/bin/bash
sudo command1
command2
sudo command3
I don't want to run command2
as root, so I won't run the script itself with sudo
. Therefore, as soon as I execute the script, sudo command1
asks for my password. Unfortunately, command2
takes about 2 hours to complete. So the sudo credentials timestamp timed out and when the script reaches sudo command3
, I'm prompted for the password again.
I don't want to permanently disable the sudo credentials timestamp timeout altogether as described in https://superuser.com/a/149740 for example. I just want to disable the timeout temporarily, effectively keep the credentials for this one bash session until it ends.
Best Answer
I think your best bet is a (backgrounded) busy-wait loop that refreshes the sudo timestamp, which you then kill when you no longer need
sudo
privileges. Here's a sample script; it runssleep 6
instead of your two-hourcommand2
and runs visible/bin/echo
commands instead ofcommand1
andcommand2
:For your actual script, I would recommend a looser loop:
Adjust the
sleep 59
to taste, depending on the existingtimestamp_timeout
setting in sudoers; I chose 59 just to demonstrate a choice you could make if the timestamp timeout was 60 seconds.