I have a long running Bash script that I don't want to run as root, but it needs root access periodically throughout. I solved this by asking for the user for the root password using
sudo -v
and then I backgrounded a process that would loop and reset the sudo timer using
sudo -n true
I then started to have weird problems when using read
in the main process. Here is a minimal script exhibiting this problem. If you run it and don't input anything before the sudo -n true
is run, the read gets a read error: 0: Resource temporarily unavailable
#!/usr/bin/env bash
sudo -v # ask user for password
sleep 1 && sudo -n true & # background a process to reset sudo
printf "Reading text: "
read -n 1 TEXT
echo "Read text: $TEXT"
I haven't been able to replicate this behavior with any other command besides sudo
. How can I run sudo -n true
in the background without interfering with read
?
Edit:
I only get this problem on Ubuntu not macOS.
sudo -v
again instead ofsudo -n true
?