There's a BASH script that might need more than 5 minutes to run a list of commands (majority are sudo)...What's the alternative?
Some say...
Use sudo -v
seems like a good option, although, after reading Apple's sudo(8) Man Page , it was said...
If given the -v (validate) option, sudo will update the user's time stamp, prompting for the user's password if necessary. This extends the sudo timeout for another 5 minutes (or whatever the timeout is set to in sudoers) but does not run a command.
I'm concerned with prompting for the user's password if necessary . What do they mean "if necessary"?
If I have sudo -v in a bash script that, let's say, executes roughly 4 minutes since the last time sudo was used (1 minute left on timestamp). Thus, giving us another 5 minutes to the timestamp - Right?
As for the other option, someone mentioned to use a loop command for updating a timestamp.
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &
Can the above code work?
I might be late to answering my own question, I did however, found a solution.
replace userpassword with your password :
echo "userpassword" | sudo -S -v
Use variable :
echo "$var" | sudo -S -v
or
If you exceed 5 minutes, use The while loop :
while true; do sudo -v; sleep 300; kill -0 "$$" || exit; done 2>/dev/null &
Use the following:
sudo -nv
This command will output nothing if sudo rights are still available for the current context. If they are not available the command will output:
$ sudo -nv
sudo: a password is required
This way it will not ask and you can build your script around these output conditions.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.