Hi,
I've recently released the os-service module to npm:
This module provides a consistent interface for installing node.js programs as operating system services.
On Windows this module uses the WIN32 API to add and remove the service, and to connect to the Windows Service Control Manager to run as a background service when started using the Windows Service Controller program, or "net start".
On Linux this module will add and remove start/stop scripts to/from the "/etc/init.d" directory, and use "chkconfig" or "update-rc.d" to add and remove the program. Then the "service ... start/stop/etc." command can be used to control the service.
From the node.js programs perspective the same code is used on both platforms, e.g.:
if (process.argv[2] == "--add") {
service.add ("my-service", {programArgs: ["--run"]}, function(error){
if (error)
console.trace(error);
});
} else if (process.argv[2] == "--remove") {
service.remove ("my-service", function(error){
if (error)
console.trace(error);
}););
} else if (process.argv[2] == "--run") {
var logStream = fs.createWriteStream (process.argv[1] + ".log");
service.run (logStream, function () {
service.stop (0);
});
// Run your background program code here...
} else {
// Show usage...
}
This can be useful when deploying an application across Windows and Linux, in that no platform specific code needs to be written. If you have a product built and packaged on a build server, during the install of the product you could install one or more services from an installation script once the installation directory had been selected and files copied in place - assuming code similar to the above was in the my-service.js script quoted below:
rem Windows
set DIR=c:\projects\my-service-project
%DIR%\node.exe %DIR%\my-service.js --add
net start my-service
# Linux
DIR=/projects/my-service-project
$DIR/node $DIR/my-service.js --add
service my-service start
This module is similar to the windows-service module I previously released:
The API's are almost identical, only the service add and remove API's are slightly different in that they now take a callback. While they are not 100% compatible I have been sure to keep them aligned, since I think the API was already in a good state, and I wanted to make it easy to migrate to this module.
Although this module essentially replaces that module, I will continue to support the windows-service module until there is no demand for it.
This is a new module, and the API for Linux is new. So there may be things that make sense to Linux that are missing, e.g. which runlevels to start/stop the service. So if there are any suggestions like this, or feedback, it will be well received.
Steve