Prasanth
Prasanth

Reputation: 35

Unix redirection issue: </dev/null 1>&- 2>&- &

Unix redirection:

Recently I faced an issue where one of the script was using the command below to execute it in background. The issue was that the script was executing twice when it is started.

For example:

In the script I put an echo "Hello" to print to log file. When the script executed I saw in the log file that it printed twice at the same time. Can any one tell me what caused here to execute the script twice.

nohup <runScript> </dev/null 1>&- 2>&- &

Upvotes: 1

Views: 445

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 754710

The original version of your question was slightly confusing. The subject line asks about (with command and argument inferred):

somecmd arg1 </dev/null 1>&- 2>&- &

The body of the question appeared to ask about:

nohup &- 2>&- &

which could reasonably be inferred to mean:

nohup somecmd arg1 &- 2>&- &

The edited version of your question is also confusing — though the change was just to indent code fragment. The notation <runscript> is ill-chosen when you are asking about I/O redirections. I'm guessing that what you wrote as <runscript> is equivalent to me writing somecmd, rather than redirecting standard input from runscript and an ill-formed output redirection. However, the revised code does at least match the subject line:

nohup runScript </dev/null 1>&- 2>&- &

So, I'll ignore the &- notation (a previous version of this answer did not).

Notation </dev/null 1>&- 2>&- &

The first command line redirects standard input from /dev/null, and closes both standard output and standard error and executes the command in background. Redirecting from /dev/null is good; closing standard output and standard error is not so good — programs are entitled to have those three file descriptors open, and that can be done by redirecting to /dev/null too:

somecmd arg1 </dev/null >/dev/null 2>&1 &

or:

somecmd arg1 </dev/null >/dev/null 2>/dev/null &

There is not much difference between these two.

Double running

There is nothing in any of the code that would account for the script being run twice, or the output appearing in a log file twice. Since you have not shown the script that was run, we cannot deduce any cause from that. On the whole, the charge would be 'operator error' — you managed to run the command twice. If you want us to look into that, you'll have to provide a reproducible script that:

  • Shows the script to be run.
  • Empties the log file.
  • Runs the script once with your chosen notation.
  • Shows that the log file contains two entries.

Without such a reproducible script, there's nothing anyone can do to help you.

Upvotes: 1

Related Questions