Start a node process in the background.
node-prefork seeks to provide an easy API for starting a node.js process in
the background, independent of any controlling terminal session.
This module will not currently work on Windows, at all - but it will, soon. We're working on it.
This module is called
prefork for a reason. When using
fork(2) on a unix
system, the current process is cloned, file descriptors and all - however, the
new child process is single-threaded. If you open a file asynchronously in
node.js, it gets opened by a thread pool; if you fork after doing this, you
cannot rely upon that file descriptor to still exist, since the new forked
child is now single-threaded.
This module does not provide any sort of process-watching functionality. While
it could certainly be used to build a process watcher, your application is on
its own after being started with
This leaves us with a few simple rules:
prefork()as close to the start of your program as is possible.
prefork()is called will reliably exist afterwards.
If these rules are followed, everything should be nice and stable.
Installation is easy via
npm install prefork
If you want the git repo:
git clone https://github.com/AvianFlu/node-prefork cd node-prefork node-gyp configure build
If you do not have
node-gyp installed, simply substitute
node-waf in the
prefork is easy - just require it, and call it before your main
application logic starts.
var http = require'http'prefork = require'prefork';var pid = prefork;httpcreateServerresend'I am a backgrounded HTTP server, running as ' + pid;listen8080;
prefork() will perform all necessary actions to background your process, and
return the new
prefork also allows for various configurations of redirected
To simply log all output to a file:
var pid = preforkstdout: 'output.log';console.log'Started successfully, new pid is %d' pid;
stdout is provided,
stderr will be redirected to the same place.
stdout, provide a
stderr option as well.
preforkstdout: 'output.log'stderr: 'errors.log';
stdin option is provided, then the file is opened, read, and its data
becomes available as data events on the
process.stdin stream. This simple
example would log the data from
inputfile to the process' new
in this case is redirected to
preforkstdin: 'inputfile'stdout: 'outputfile';processstdinresume;processstdinon'data'console.logdatatoString;;
If you're feeling adventurous, an array of integer file descriptors can also be
prefork(). Note that this will override any filename options that
have been passed - note also that passing random integers that aren't real
file descriptors is not a good idea.
var fd = require'fs'openSync'output.log' 'a';preforkcustomFds: -1 fd -1;
If a value of
-1 is passed for any of the file descriptors, as in the example
above, that file descriptor will be nulled. This example nulls
stderr, but redirects
stdout to a file.