Bash: how can I run `sudo -n true` in the background without interfering with `read`?

Ross MacArthur

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.

Stéphane Chazelas

I get the same behaviour with:

sleep 1 && true < /dev/tty &
read var

sudo opens /dev/tty to query the current foreground process group, that causes the read() system call made by bash's read to return with EAGAIN with Ubuntu 18.04's Linux kernel 4.15.0-45-generic and 4.18.0-14-generic at least causing the read utility to return with that error.

That seems to be caused by a bug in recent versions of the Ubuntu variants of the Linux kernel. I can't reproduce it on Solaris nor FreeBSD, nor in any version of Linux on Debian (though I can reproduce it if I boot Debian on Ubuntu's 4.18 kernel).

https://bugs.launchpad.net/ubuntu/+source/linux-signed-hwe/+bug/1815021 seems to be another manifestation of that bug.

That's introduced by https://lkml.org/lkml/2018/11/1/663 which ubuntu backported to both its 4.15 and 4.18 kernels at least. But Ubuntu had not backported another change that fixes a regression introduced by that patch until 2 hours ago.

4.18.0-15-generic has now landed in the Ubuntu repositories and fixes the issue. I suppose the one for 4.15 will follow shortly.

ksh93 doesn't have the problem for that same code as its read builtin uses select() first to wait for input, and select() doesn't return when the other process opens /dev/tty.

So here you could use ksh93 instead of bash or wait for the fixed kernel or go back to 4.15.0-43 until 4.15.0-46 is released.

Alternatively, you could use zsh which has builtin support for changing uids (via the EUID/UID/USERNAME special variables) provided you start the script as root so you wouldn't need to run sudo within the script (it's also potentially dangerous to extend the life of the sudo token longer than the user would expect).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I run NPM without sudo?

From Dev

How can I run /usr/bin/Xorg without sudo?

From Dev

How can I run /usr/bin/Xorg without sudo?

From Dev

How can I run pip without a sudo (for mac)? It doesnt work without sudo

From Dev

How can I run Java without GUI (where isHeadless is true)?

From Dev

How can I run all the bash process as background?

From Dev

How can I run a command at the background without ampersand (&)?

From Dev

Why can I not run an application installed using sudo without sudo?

From Dev

How can I run Windows bash without being root?

From Dev

How can I run a bash script without affecting the file system?

From Dev

How can I run bash without running initialization/configuration scripts?

From Dev

How can I run vscode with sudo on osx?

From Dev

How can I run growisofs via sudo?

From Dev

How can I use docker without sudo?

From Dev

How can I run commands in a bash script prefixed with sudo, but based on a flag?

From Dev

How can I change a label text without interfering with an input radio inside the label?

From Dev

How can I play video, fullscreen, without keyboard and mouse interfering, on Linux

From Dev

In Angular2+, how can I replace a <tr> with my own component without interfering with the dom structure/css

From Dev

How do I run a sudo command needing password input in the background?

From Dev

How do I run a sudo command needing password input in the background?

From Dev

Why do I need a tty to run sudo if I can sudo without a password?

From Dev

How can I run a Java program in background?

From Dev

How do I run specific sudo commands without a password?

From Dev

How can I execute a script that needs sudo permission in the background?

From Dev

How can I run a bash variable as a command exactly without additional quotation?

From Dev

how can I run sudo commands as exec resources in puppet

From Dev

How can I run detached command with sudo over ssh?

From Dev

How can I run a script as sudo and for it to not ask me for a password?

From Dev

Docker-compose: cannot build WITHOUT sudo but I can run containers without it

Related Related

  1. 1

    How can I run NPM without sudo?

  2. 2

    How can I run /usr/bin/Xorg without sudo?

  3. 3

    How can I run /usr/bin/Xorg without sudo?

  4. 4

    How can I run pip without a sudo (for mac)? It doesnt work without sudo

  5. 5

    How can I run Java without GUI (where isHeadless is true)?

  6. 6

    How can I run all the bash process as background?

  7. 7

    How can I run a command at the background without ampersand (&)?

  8. 8

    Why can I not run an application installed using sudo without sudo?

  9. 9

    How can I run Windows bash without being root?

  10. 10

    How can I run a bash script without affecting the file system?

  11. 11

    How can I run bash without running initialization/configuration scripts?

  12. 12

    How can I run vscode with sudo on osx?

  13. 13

    How can I run growisofs via sudo?

  14. 14

    How can I use docker without sudo?

  15. 15

    How can I run commands in a bash script prefixed with sudo, but based on a flag?

  16. 16

    How can I change a label text without interfering with an input radio inside the label?

  17. 17

    How can I play video, fullscreen, without keyboard and mouse interfering, on Linux

  18. 18

    In Angular2+, how can I replace a <tr> with my own component without interfering with the dom structure/css

  19. 19

    How do I run a sudo command needing password input in the background?

  20. 20

    How do I run a sudo command needing password input in the background?

  21. 21

    Why do I need a tty to run sudo if I can sudo without a password?

  22. 22

    How can I run a Java program in background?

  23. 23

    How do I run specific sudo commands without a password?

  24. 24

    How can I execute a script that needs sudo permission in the background?

  25. 25

    How can I run a bash variable as a command exactly without additional quotation?

  26. 26

    how can I run sudo commands as exec resources in puppet

  27. 27

    How can I run detached command with sudo over ssh?

  28. 28

    How can I run a script as sudo and for it to not ask me for a password?

  29. 29

    Docker-compose: cannot build WITHOUT sudo but I can run containers without it

HotTag

Archive