1

Assume that I am connected via SSH on a remote Ubuntu system. I want to execute a script there using nohup in case of potential connection drops and redirecting its log to a file. The command I run is

nohup bash myscript.sh > log.txt &

Then, the terminal prints the PID of the script and the message:

nohup ignoring input and redirecting stderr to stdout

The cursor freezes there and I am not able to execute other commands unless I exit the SSH session with Ctrl + D and relogin. I would prefer the SSH session to remain open and proceed to the next terminal prompt without the need to relogin. Is there any way to achieve this?

2
  • 2
    If you press Enter once do you get the prompt back? Aug 20, 2020 at 6:12
  • Yes. I was expecting that I had missed something trivial. Thanks.
    – mgus
    Aug 20, 2020 at 20:06

2 Answers 2

1

This command runs in the background as you'd expect. However, in this particular instance there's a race condition where the output message is written fractionally after the shell prompt.

The visual result is either that the shell prompt precedes the nohup message or that it's completely overwritten.

The practical result is that because the shell has written out its prompt it's ready for the next command. You can press Enter to generate a new prompt, or just type the next command in anyway.

0

Add this to .bashrc

function detach { ( nohup $* </dev/null 2>&1 >/dev/null & disown ; sleep 0 ) >/dev/null ; } ; export -f detach

Then you can run: detach e.g. detach gedit .bashrc

You can also make functions for specific programs, such as:

function gedit { ( nohup gedit $* </dev/null 2>&1 >/dev/null & disown ; sleep 0 ) >/dev/null ; } ; export -f gedit

The "disown" prevents output when the program ends in the background.

The "sleep 0" causes the prompt to return.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .