0

This is causing problems:

node "$SUMAN_PROJECT_ROOT/server.js" | bunyan -o short &> ${LOG_FILE}  &

this works:

node "$SUMAN_PROJECT_ROOT/server.js"  &> ${LOG_FILE}  & 

the problem is that with the first command, the output is not being sent to the file. In the second command, it does get sent to the file. Why might it be the case the first command does not work - I would expect it would send the output to the file just like the second command. The bunyan command should act like grep, just filter and mapping the output from the node command.

My editor does not like a proposed solution:

enter image description here

  • 1
    Change your | to &| and see the question I've linked for why. – Wildcard Sep 06 '17 at 04:20
  • because it would have been so easy to search for this... – Alexander Mills Sep 06 '17 at 06:02
  • 3
    Well, if you really understand what are the parts of the commands you are using, then yes, it's easy to search for. If you don't know what the | and the &> are for, and just trust that it will all work somehow, that's called "cargo cult programming." It is important to understand fully what the code does that you write. I don't really mean to criticize you; I'm glad you came to this site, because "cargo cult" describes about 99% of the production shell code I've seen. So it's not just you. :) – Wildcard Sep 06 '17 at 06:09
  • I know what the | and &> are for...but I still do not know why it doesn't work in the first example. I wrote these shell commands, no copy paste lol. – Alexander Mills Sep 06 '17 at 06:21
  • I have done a decent amount of bash programming and I have never seen or used &| – Alexander Mills Sep 06 '17 at 06:23
  • 1
    ↑ then I suggest you read @Wildcard's linked answer to find out about it. An alternative construct is 2>&1 | – Chris Davies Sep 06 '17 at 10:51
  • 1
    Is the question now about why your editor doesn't like it, or that the command doesn't actually work? – Jeff Schaller Sep 06 '17 at 19:09

1 Answers1

6
node "$SUMAN_PROJECT_ROOT/server.js"  &> ${LOG_FILE}  & 

First let's examine the working command.

This says to take the standard output AND standard error of the node command, and redirect both to the log file. (And you need to quote your variable; it will break if LOG_FILE includes whitespace. See Why does my shell script choke on whitespace or other special characters?)

And the command is put in the background with & but that's not really relevant.

node "$SUMAN_PROJECT_ROOT/server.js" | bunyan -o short &> ${LOG_FILE}  &

This command you say doesn't work. What this does is take the standard output ONLY of the node command and pass it to the bunyan command, then redirect the standard output and standard error of the bunyan command to the log file.

Since you say this doesn't work, the obvious conclusion is that the output you are interested in from the node command is being sent to its standard error, not its standard output. Use &| or use node ... 2>&1 | ... so the standard error is redirected as well.

(&| is a bashism, not portable, but so is &> so I imagine you don't care about that.)

Wildcard
  • 36,499
  • That don't make no damn sense! :) node x.js | bunyan sends both stdout and stderr to bunyan. So why would I need to do node x.js &| bunyan? – Alexander Mills Sep 06 '17 at 18:56
  • In this answer it would help to add a line demonstrating the "final answer in action". – Alexander Mills Sep 06 '17 at 18:58
  • I added a screenshot to the original question showing my current woes given you suggestions. I am sure we can get to the bottom of this :) – Alexander Mills Sep 06 '17 at 19:00
  • 1
    @AlexanderMills, "node x.js | bunyan sends both stdout and stderr to bunyan" — no, it doesn't. I don't have to try to convince you; the wonderful thing about facts is they are true whether you believe them or not. And your editor is wrong; you should file a bug report with the editor developers. – Wildcard Sep 06 '17 at 19:14
  • yeah you're right about the pipe receiving only stdout, duh, my bad – Alexander Mills Sep 06 '17 at 19:26
  • 1
    the nice things about facts is they tell us that communism doesn't work – Alexander Mills Sep 06 '17 at 19:31