2

I am running my node program like this

sudo NODE_ENV=production node app.js&

Apparently, everything works fine and it works even if the terminal is closed. If terminal that run the program is opened, output will be shown on the terminal.

It occasionally output some data, however, I cannot see it if original terminal was not opened, and unless I wait for the output 24 hours full time.

How can I redirect every string output from the program to specific file? so that I will be able to find out the possible cause of exception or unexpected termination of a node program?

Ricardo Tomasi
  • 34,573
  • 2
  • 55
  • 66
user482594
  • 16,878
  • 21
  • 72
  • 108

3 Answers3

4

just tack on >> file

sudo NODE_ENV=production node app.js >> log.txt &
0

use forever for example -

forever start -l forever.log -o out.log -e err.log my-daemon.js

where

-l LOGFILE Logs the forever output to LOGFILE -o OUTFILE Logs stdout from child script to OUTFILE -e ERRFILE Logs stderr from child script to ERRFILE

Here you can find all the documentation needed.

jaksoni
  • 1
  • 1
0
sudo nohup node app.js > app_logs.out

You can also use NODE_ENV=production in the example above if you want. nohup refers to 'no hangup'.

Resources:

Node.js as a background service
sudo nohup nice <-- in what order?

Community
  • 1
  • 1
alessioalex
  • 62,577
  • 16
  • 155
  • 122