*// index.js
process.chdir(__dirname);
var fs = require ("fs");
const os = require('os');
var mpath = require.resolve('os-service');
function getUserHome() {
// From process.env
return process.env[(process.platform == 'win32')
? 'USERPROFILE' : 'HOME'];
}
process. env['HOME'] = getUserHome() ;
var service = require('nexe-natives')(mpath);
function usage () {
console.log ("usage: node periodic-logger --add <name> [username] [password] [dep dep ...]");
console.log (" node periodic-logger --remove <name>");
console.log (" node periodic-logger --run");
process.exit (-1);
}
//console.log(process.argv);
if (process.argv[2] == "--add" && process.argv.length >= 4) {
var options = {
programArgs: ["--run", "me"]
};
if (process.argv.length > 4)
options.username = process.argv[4];
if (process.argv.length > 5)
options.password = process.argv[5];
if (process.argv.length > 6)
options.dependencies = process.argv.splice(6);
service.add (process.argv[3], options, function(error) {
console.log("SERVICE NAME:" + process.argv[3] + " INSTALLED");
if (error)
console.log(error.toString());
});
} else if (process.argv[2] == "--remove" && process.argv.length >= 4) {
console.log("remove");
service.remove (process.argv[3], function(error) {
if (error)
console.log(error.toString());
});
} else if ((process.argv[2] == "--run") || (process.argv[3] == "--run")) {
console.log("run");
service.run (logStream, function () {
console.log("runn service called")
service.stop (0);
});
var logStream = fs.createWriteStream(process.argv[1] + ".log");
// Here is our long running code, simply print a date/time string to
// our log file
setInterval (function () {
logStream.write(new Date ().toString () + "\n");
}, 1000);
} else {
console.log("else");
usage ();
}*
the below command runs the application and also install the window-service named "periodic-logger-service" with startup type "Automatic" and i am able to see the service created in window-services manager and if we need to start this window-service we have to start it manually or we need to reboot the machine.
>> node index.js --add periodic-logger-service
But I want to start this window service automatically from command prompt by passing the run arguments, tried below command as mentioned in the os-service readme docs
>>node index.js --run
this starts the application and the logging is written in the index.log file and when I exit the terminal by cntrl C the application stops, and the window-service ("periodic-logger-service") installed is not running.
Kindly help to resolve this issue. Also wanted to know how to check whether the window-service is already installed or not for below requirements.
- if already installed, then i need to run the service
- if not installed, then i need to install and run the service.
reference taken - https://www.npmjs.com/package/os-service