Question

I have a node.js app which runs until user quits the process. I'm trying to run this app as a background process with forever, however, my app originally outputs something to stdout:

process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write('...');

and if I run this app with forever

forever start -l forever.log -o out.log -e err.log app.js

it does run in the background but there's nothing in out.log file. If I look at err.log I see that problem is in printing something to stdout:

$ cat err.log 

/Users/err/Sites/js/test/app.js:105
    process.stdout.clearLine();
                   ^
TypeError: Object #<Socket> has no method 'clearLine'
    at null.<anonymous> (/Users/err/Sites/js/test/app.js:105:20)
    at wrapper [as _onTimeout] (timers.js:252:14)
    at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)

I also tried to use forever-monitor and wrote a small script:

var config = require('./config');

if (config.runInBackground) {
  var forever = require('forever-monitor');
  var child = new (forever.Monitor)('app.js', {
    max: 1,
    silent: true,
    outFile: 'monitor.out.log',
    errFile: 'monitor.err.log'
  });
  child.on('stdout', function(e) {
    console.log(e);
  });
  child.on('exit', function () {
    console.log('exited.');
  });
  child.start();
} else {
  // do the regular process.stdout.write('...');
}

But my script exists without writing any file or starting a background process.

How can I run my node app in the background and write the original stdout to some log file? Or even better, is it possible to have an option (let's say in config.js file) to run this app as a background process or not, and if so, all stdout stuff should be written to a file?

Was it helpful?

Solution

You may need to specify an absolute path for your files.

That said, there are alternatives to forever that might work depending on your operating system. node-windows and node-mac both offer what you're looking for. node-linux isn't ready yet, but the project has some init.d scripts that could be configured to run processes with logging.

Disclosure: I am the author of all three of these.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top