在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):coreybutler/node-linux开源软件地址(OpenSource Url):https://github.com/coreybutler/node-linux开源编程语言(OpenSource Language):JavaScript 82.5%开源软件介绍(OpenSource Introduction):node-linuxSponsors (as of 2020) Follow the author on Twitter (@goldglovecb). Contributions Requested (see below) Documentation is available at the node-linux portal. This is a standalone module, originally designed for internal use in NGN. However; it is capable of providing the same features for Node.JS scripts independently of NGN. For alternative versions, see node-windows and node-mac This module makes it possible to daemonize Node.js scripts natively (using systemv init.d scripts). To start, install node-linux via:
node-linux has a utility to run Node.js scripts as Linux daemons. To create a service with node-linux, prepare a script like: var Service = require('node-linux').Service;
// Create a new service object
var svc = new Service({
name:'Hello World',
description: 'The nodejs.org example web server.',
script: '/path/to/helloworld.js'
});
// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
svc.start();
});
svc.install(); The code above creates a new The
In the example above, the script listens for the Services created by node-linux are like other services running on Linux.
They can be started/stopped using Environment VariablesSometimes you may want to provide a service with static data, passed in on creation of the service. You can do this by setting environment variables in the service config, as shown below: var svc = new Service({
name:'Hello World',
description: 'The nodejs.org example web server.',
script: '/path/to/helloworld.js',
env: {
name: "HOME",
value: process.env["USERPROFILE"] // service is now able to access the user who created its' home directory
}
}); You can also supply an array to set multiple environment variables: var svc = new Service({
name:'Hello World',
description: 'The nodejs.org example web server.',
script: '/path/to/helloworld.js',
env: [{
name: "HOME",
value: process.env["USERPROFILE"] // service is now able to access the user who created its' home directory
},
{
name: "TEMP",
value: path.join(process.env["USERPROFILE"],"/temp") // use a temp directory in user's home directory
}]
}); Setting run as user/groupBy default your node service will run as root:root. You may not want that. Just pass the requested user/group values at startup var svc = new Service({
name:'Hello World',
description: 'The nodejs.org example web server.',
script: '/path/to/helloworld.js',
user: "vagrant",
group: "vagrant"
}); Cleaning Up: Uninstall a ServiceUninstalling a previously created service is syntactically similar to installation. var Service = require('node-linux').Service;
// Create a new service object
var svc = new Service({
name:'Hello World',
script: require('path').join(__dirname,'helloworld.js')
});
// Listen for the "uninstall" event so we know when it's done.
svc.on('uninstall',function(){
console.log('Uninstall complete.');
console.log('The service exists: ',svc.exists());
});
// Uninstall the service.
svc.uninstall(); The uninstall process only removes process-specific files. It does NOT delete your Node.js script, but it will remove the logs! What Makes node-linux Services Unique?Lots of things! Long Running Processes & Monitoring: There is no built-in service recovery in most Linux environments, and third party products can be fairly limited or not easily configured from code. Therefore, node-linux creates a wrapper around the Node.js script. This wrapper is responsible for restarting a failed service in an intelligent and configurable manner. For example, if your script crashes due to an unknown error, node-linux will attempt to restart it. By default, this occurs every second. However; if the script has a fatal flaw that makes it crash repeatedly, it adds unnecessary overhead to the system. node-linux handles this by increasing the time interval between restarts and capping the maximum number of restarts. Smarter Restarts That Won't Pummel Your Server: Using the default settings, node-linux adds 25% to the wait interval each time it needs to restart
the script. With the default setting (1 second), the first restart attempt occurs after one second.
The second occurs after 1.25 seconds. The third after 1.56 seconds (1.25 increased by 25%) and so on.
Both the initial wait time and the growth rate are configuration options that can be passed to a new
var svc = new Service({
name:'Hello World',
description: 'The nodejs.org example web server.',
script: '/path/to/helloworld.js'),
wait: 2,
grow: .5
}); In this example, the wait period will start at 2 seconds and increase by 50%. So, the second attempt would be 3 seconds later while the fourth would be 4.5 seconds later. Don't DOS Yourself! Repetitive recycling could potentially go on forever with a bad script. To handle these situations, node-linux
supports two kinds of caps. Using Finally, an attribute called How Services Are Madenode-linux uses the templates to generate init.d scripts for each Node.js script deployed as a
service. This file is created in Event Logging A log source named By default, any ContributionsDue to some unforeseen life circumstances, I was not able to add all of the features I'd hoped to add before releasing this. I'll chip away at them over time, but I would be very interested in community contributions in the following areas:
I have also added a tag in the issues called If you are interested in working on one of these features, please get in touch with me before you start to discuss the feature. License (MIT)Copyright (c) 2013 Corey Butler Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论