1

I have a script that allows me to perform a number of automated tasks. At the end of the script, I run the following command:

nohup ./MyServer &

This command launches in the background my server which has a console. Nohup will then create a nohup.out file that will contain the content of the standard console output. When the enter key is pressed, the console displays TC>\n.

The problem is that running the nohup command from a script will cause the enter key to stay pressed in the console.

This means that running the nohup command from a script will make millions of writes per second to the nohup.out file.

However, if I simply run the command

nohup ./MyServer &

These writings do not occur and I can see at any time the potential errors in the nohup.out file.

Do I have to run my custom script that launches the automated tasks, wait for the end of the execution of this script (which takes time) and then run the nohup command to launch my server?

1 Answer 1

0

If you're running that on a machine with GNU nohup (e.g. desktop/server Linux), nohup will not only set the SIGHUP signal's disposition to "ignore", and redirect the stdout/stderr to nohup.out, but, if its stdin is a terminal, also redirect the stdin from /dev/null opened in write-only mode, so that any read will result in an error.

When a background command is run from a script, e.g. foo &, its stdin is already redirected from /dev/null opened in read-only mode, that redirection will not happen, and any read from it will result in EOF instead of an error.

So you can simulate from a script nohup's behaviour in a terminal with:

(trap '' HUP; command 0>/dev/null >logfile 2>&1 &) 
4
  • Thanks for your time, but I don't understand why it only bugs when the nohup command is run from a script. While the same command launched from the shell doesn't cause any error, do you know what is the cause of this bug? Commented Oct 13, 2020 at 18:29
  • It may be related to this (bg commands in scripts have their stdin redirected from /dev/null), so it looks that my answer may be way off. Though without knowing what your ./MyServer does it would be hard to guess. But anyways, try running the setsid example (before I remove my answer).
    – user313992
    Commented Oct 13, 2020 at 18:34
  • I tried both solutions, unfortunately neither of them work. Commented Oct 13, 2020 at 18:50
  • Try this from your script: (trap '' HUP; ./MyServer 0>/dev/null >logfile 2>&1 &), which should reproduce what nohup is doing when running from a terminal.
    – user313992
    Commented Oct 13, 2020 at 18:51

You must log in to answer this question.

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