How to add NodeJS process to windows
There’re a few ways to add NodeJS process to windows server.
Install node-windows
npm install -g node-windows
Then Daemonizing your script is accomplished through code:
var Service = require('node-windows').Service; // Create a new service object var svc = new Service({ name:'Hello World', description: 'The NodeJS web server.', script: 'C:\\path\\to\\my\\node\\script.js' }); // Listen for the "install" event, which indicates the // process is available as a service. svc.on('install',function(){ svc.start(); }); // Listen for the "start" event and let us know when the // process has actually started working. svc.on('start',function(){ console.log(svc.name+' started!\nVisit http://localhost:3001 to see it in action.'); }); // Install the script as a service. svc.install();
Or simply use the daemon npm module
var daemon = require('daemon'); daemon.daemonize({ stdout: './log.log' , stderr: './log.error.log' } , './node.pid' , function (err, pid) { if (err) { console.log('Error starting daemon: \n', err); return process.exit(-1); } console.log('Daemonized successfully with pid: ' + pid); // Your Application Code goes here });
Or put nodejs MyServer.js in a .sh file, and nohup sh startserver.sh
Or use this package built by talles
npm install -g qckwinsvc Installing your service: > qckwinsvc prompt: Service name: [name for your service] prompt: Service description: [description for it] prompt: Node script path: [path of your node script] Service installed Uninstalling your service: > qckwinsvc --uninstall prompt: Service name: [name of your service] prompt: Node script path: [path of your node script] Service stopped Service uninstalled
Or install os-service here
npm install os-service var service = require ("os-service"); //A program can then be added, removed and run as a service: service.add ("my-service"); service.remove ("my-service"); var logStream = fs.createWriteStream ("my-service.log"); service.run (logStream, function () { console.log ("stop request received"); service.stop (); });