How may I provide my password repeatedly to a script contaning a loop of sudo?

I have a script which contains

while true
  sudo mycmd
  sleep 10000
  [ ... ] ; break
end

when I run the script in bash, I will have to provide my password once in 10000 seconds after finishing running the previous instance of sudo mycmd.

I remeber yes | somecommand can repeatedly provide yes as stdin input to somecommand, as answers to repeated questions of “Yes or No”. I wonder how I can provide my password repeatedly to the script?

Thanks.

Answer

The password can be piped into the sudo command but this is not secure in any way at all and should be avoided if possible.

echo 'hunter2' | sudo -S mycmd

A better method would be to just run the entire script with sudo and only require the password once.

The sudo password can also be avoided entirely but is also not recommended for security reason. This can be done by updating the /etc/sudoers file with the visudo command and including a line such as this for all users in the wheel group.

%wheel ALL=(ALL) NOPASSWD: ALL

To disable it only for a single user switch %wheel with the users username.

Attribution
Source : Link , Question Author : Tim , Answer Author : LinuxSecurityFreak

Leave a Comment