0

Context

I am using this way of elevating permissions in a script if not run as a root.

My Bash script checks for the presence of a certain file it needs to use:

if [ $EUID != 0 ]; then
    if [ -f "/foo/bar" ]
    then
        AGE=$(expr `date +%s` - `stat -c %Y "/foo/bar"`)
        echo "/foo/bar is here, ${AGE}s old"
    else
        echo "/foo/bar not there (yet?)"
    fi

    sudo "$0" "$@"
    exit $?
fi

# the rest of the script, needs /foo/bar

So if a non-root runs this, and there is the file, the response is like this /foo/bar is here, 27s old. Otherwise, the response is /foo/bar is not there (yet?). Then, the sudo prompt runs.

Now the question is: in the second case, when /foo/bar is not ready yet, is there a way to notify the user once /foo/bar appears? The user would type the password and see "oh the file is there" or "better wait before I hit Return".

There are two parts of this, detect a file has appeared in the file system (not asking about this one), and echo something while the sudo prompt is up.

Question

I am interested in the second part. Is it possible to write something to the output, above or below the sudo prompt, "asynchronously"?

  • 2
    Use inotify to detect that the file has appeared. – Satō Katsura Jun 28 '16 at 9:39
  • Question: Why display the sudo prompt at all if the user is not meant to press RETURN at that point? Why not just wait until the file is ready and then run sudo? – Stephen Harris Jun 28 '16 at 11:37
  • The file is going to appear in the span of the few seconds while the user is typing the password. The sequence of steps could be rearranged I guess, first ask for sudo, then wait until the file appears - meaning "don't foo the bar" would be a valid answer here. However, I really do want to find out if it's possible to asynchronously let the user know while the sudo prompt is there. – vektor Jun 28 '16 at 11:43
  • I posted an answer to the second part of the question. It would be nice to post that as a separate question (or edit this one to leave only the second part). – Satō Katsura Jun 28 '16 at 14:43
  • @SatoKatsura thanks, I was trying to provide some context. I made the question a bit clearer to focus on the real question. Thanks for the answer, I will try it out. – vektor Jun 28 '16 at 15:55
1

Answering the second part of the question, how to write messages while waiting at the sudo prompt:

trap '2>/dev/null' CHLD
(sleep 5; echo; echo "New spam has just arrived!") &
MSGPID=$!
sudo -v
wait $MSGPID
trap - CHLD

sleep 5 in the subshell simulates the message arriving asynchronously. The first echo just makes sure the message doesn't end up next to the Password: prompt. It also works when writing to stderr instead of stdout.

The first trap is there to kill the messages about the status of the background job. waitpid and the second trap cancel the effects of the first trap.

Bad things happen if the command in the subshell exits before having a chance to return a meaningful PID (can happen on some OSes, hopefully not too many these days).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.