0

Here are 2 code examples. Both are nohup codes in a bash string. The first one does the job and second doesn't. I ask why.

sudo apt-get purge zip unzip
nohup bash -c " sleep 20s; apt-get install zip unzip; " & 

The above command is executed in the background and installas these software (I've affirmed it by executing the stdin "zip".


nohup bash -c " sleep 20s; clear; "

This command runs in the foreground but doesn't clear the tty. Why is that? What could I do to make it clear the screen of the current session? Is there any argument/way of execution/script that I could run together with the command, that might help with this?

1 Answers1

3

The clear program is writing to the standard output, which is redirected by the nohup command. So that has no effect on the terminal.

You can see the output (from clear) in nohup.out.

You could do this instead:

nohup bash -c " sleep 20s; clear >/dev/tty; "

which tells the shell to redirect the output of clear to the tty device. You could use the tty command to identify the terminal device which you are using, e.g.,

    nohup bash -c " sleep 20s; clear > $(tty); "

but in most cases both will work.

You may get a warning from nohup, which can be quieted:

    nohup bash -c " sleep 20s; clear > $(tty); " 2>/dev/null
Thomas Dickey
  • 76,765
  • @Benia nohup does not want to redirect to tty, it explicitly checks the type of the stdout (and stderr), the idea to prevent it from writing to something which might not exist anymore. – phk Dec 29 '16 at 23:17
  • @Benia, if you don't want nohup to redirect to a file, don't use nohup. – Chris Davies Dec 29 '16 at 23:29
  • @Thomas, the command works but brings ignoring input and appending output to ‘/root/nohup.out’ and then the effect is seen. BUT, if I do ctrl+c, it brakes execution (this won't happen if I don't use >/dev/tty). How could I do ctrl+c without destroying the command? –  Dec 30 '16 at 00:04
  • ctrl+c affects the command because you did not add "&" on the end, but this is getting away from the original question (there are more pitfalls with nohup to explore in other questions). – Thomas Dickey Dec 30 '16 at 00:47