0

I have a script that runs one of the following line

sudo -u $USER $SUDOCMD &>>stdout.log

The sudo command is a realtime process that print out lots of stuff to the console.

After running the script each time, the script does not return to the command prompt. You have to press enter or ctrl + c to get back to the command prompt.

Is there a way to to do it automatically, so that I can get a return value from the script to decide whether the script runs ok or failed.

thanks.

4
  • 1
    You are back at the prompt; you're just not seeing a prompt printed because a bunch of your program's output went after the prompt, so it's hidden behind all that output. Commented Mar 7, 2019 at 1:03
  • 1
    This type of thing happens if the script runs background processes that keep printing after the script returns. They write their output after the prompt. Commented Mar 7, 2019 at 1:04
  • Maybe the script needs to use the wait command so it doesn't exit until all the background processes finish. Commented Mar 7, 2019 at 1:06
  • charles barmar , yes, you are right. The process keeps running in the background and print out output to the console. I do not think we need a "solution" . Commented Mar 7, 2019 at 14:15

1 Answer 1

1

What is probably happening here is that your script is printing binary data to the TTY rather than text to standard output/error, and this is hiding your prompt. You can for example try this:

$ PS1='\$ '
$ (printf "first line\nsecond line\r" > $(tty)) &>> output.log

The second command will result in two lines of output, the second one being "mixed in" with your prompt:

first line
$ cond line

As you can see the cursor is on the "c", but if you start typing the rest of the line is overwritten. What has happened here is the following:

  1. You pressed Enter to run the command, so the cursor moved a line down.
  2. The tty command prints the path to the terminal file, something like "/dev/pts/1". Writing to this file means that the output does not go to standard output (which is usually linked to the terminal) but directly to the terminal.
  3. The subshell (similar to running the command in a shell script) ensures that the first redirect isn't overridden by the second one. So the printf output goes directly to the terminal, and nothing goes to the output log.
  4. The terminal now proceeds to print the printf output, which ends in a carriage return. Carriage return moves the cursor to the start of the line you've already written to, so that is where your prompt appears.

By the way:

Sign up to request clarification or add additional context in comments.

1 Comment

this seems not to be the case with this issue. But thanks much for your details and recommendation,

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.