18

I understand > /dev/null redirects things to /dev/null which acts like a blackhole. However, I don't understand what < /dev/null means. I saw some script written like this:

nohup myprogram > foo.out 2> foo.err < /dev/null &

So, what does < /dev/null in the code above mean?

here's an example where it's suggested

Qian Chen
  • 799

2 Answers2

20

It ensures that all I/O streams are accounted-for/occupied.

This way, the backgrounded process has nothing "tied" to the terminal so you can go about your business without your program trying to read from TTY, which would cause the terminal to hang.

In this case, since you're launching the process over ssh from a shell script, it's making sure that the script can move along unencumbered.

user3276552
  • 1,343
  • Does it assume myprogram takes some input, but in this case it feeds myprogram nothing as input? – Qian Chen Sep 20 '15 at 16:46
  • 1
    Thanks. I understand all the rest except the < /dev/null. – Qian Chen Sep 20 '15 at 16:49
  • So what's the difference between the code and the same code without < /dev/null? – Qian Chen Sep 20 '15 at 16:53
  • I actually read the code from here: http://stackoverflow.com/questions/29142/getting-ssh-to-execute-a-command-in-the-background-on-target-machine – Qian Chen Sep 20 '15 at 16:54
  • Still don't understand. Did you mean the < /dev/null makes sure that this unfinished process does not maintain any ties that will keep the session open? – Qian Chen Sep 20 '15 at 17:39
  • Aha, I finally understand. It eliminated any possibility that std in/out/err could tie to the terminal, instead explicitly redirect them to files. Thank you! – Qian Chen Sep 20 '15 at 17:44
  • 2
    glad it helped! sorry about the runaround; the context helped though, thanks! -- side note: i'm gonna delete a bunch of my comments in a minute or two, now that things are nailed down, it just looks confusing for anyone else who sees it – user3276552 Sep 20 '15 at 17:48
13

program </dev/null means that the program is taking its input argument (can be input parameter to an option or can be input file to operate on) via file descriptor 0 i.e. STDIN, from the file /dev/null.

As you already know, /dev/null contains nothing, it will notify EOF (End of File) upon read so any program taking input from /dev/null will basically redirecting nothing as its input argument.

heemayl
  • 56,300
  • So is there any difference to just leave < /dev/null out? Just write it as nohup myprogram > foo.out 2> foo.err & – Qian Chen Sep 20 '15 at 17:31
  • 1
    @ElgsQianChen That depends on your program..what does it want as STDIN ? Some programs might make it mandatory so you need to put something as STDIN, this might be the case here that you just used /dev/null as STDIN and let the program do the processing and put the STDOUT and STDERR in the files mentioned.. – heemayl Sep 20 '15 at 17:33
  • So does it effectively prevent any input from blocking the process, and just feed nothing to any input request? – Qian Chen Sep 20 '15 at 17:36
  • 1
    @ElgsQianChen It will block the file descriptor 0 i.e. STDIN so you can not pass any more input via that..if there is no other FD configured to take input from you won't be able to give any further input to the program.. – heemayl Sep 20 '15 at 17:38