{"id":13455064,"url":"https://github.com/coreybutler/node-linux","last_synced_at":"2025-04-12T19:46:44.048Z","repository":{"id":8554298,"uuid":"10178287","full_name":"coreybutler/node-linux","owner":"coreybutler","description":"Create native background daemons on Linux systems.","archived":false,"fork":false,"pushed_at":"2022-02-24T08:44:25.000Z","size":1210,"stargazers_count":419,"open_issues_count":17,"forks_count":74,"subscribers_count":21,"default_branch":"master","last_synced_at":"2024-10-29T22:32:36.540Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/coreybutler.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["coreybutler"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2013-05-20T17:48:19.000Z","updated_at":"2024-09-23T03:25:05.000Z","dependencies_parsed_at":"2022-09-17T15:40:53.292Z","dependency_job_id":null,"html_url":"https://github.com/coreybutler/node-linux","commit_stats":{"total_commits":75,"total_committers":12,"mean_commits":6.25,"dds":0.5066666666666666,"last_synced_commit":"8c54ca888298d1a5ad3c891da7e867996b89dba8"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coreybutler%2Fnode-linux","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coreybutler%2Fnode-linux/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coreybutler%2Fnode-linux/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coreybutler%2Fnode-linux/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/coreybutler","download_url":"https://codeload.github.com/coreybutler/node-linux/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248625497,"owners_count":21135513,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T08:01:00.869Z","updated_at":"2025-04-12T19:46:44.017Z","avatar_url":"https://github.com/coreybutler.png","language":"JavaScript","readme":"# node-linux\n\n[![NPM version](https://badge.fury.io/js/node-linux.png)](http://badge.fury.io/js/node-linux)\n[![NGN Dependencies](https://david-dm.org/coreybutler/node-linux.png)](https://david-dm.org/coreybutler/node-linux)\n[![Build](https://api.travis-ci.org/coreybutler/node-linux.png)](https://travis-ci.org/coreybutler/node-linux)\n\n**Sponsors (as of 2020)**\n\n\u003ctable cellpadding=\"10\" cellspacing=\"0\" border=\"0\"\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ca href=\"https://metadoc.io\"\u003e\u003cimg src=\"https://github.com/coreybutler/staticassets/raw/master/sponsors/metadoclogobig.png\" width=\"200px\"/\u003e\u003c/a\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003ca href=\"https://butlerlogic.com\"\u003e\u003cimg src=\"https://github.com/coreybutler/staticassets/raw/master/sponsors/butlerlogic_logo.png\" width=\"200px\"/\u003e\u003c/a\u003e\u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nFollow the author on [Twitter (@goldglovecb)](http://twitter.com/goldglovecb).\n\n**Contributions Requested**\n\n(see below)\n\nDocumentation is available at the [node-linux portal](https://coreybutler.github.io/node-linux).\n\nThis is a standalone module, originally designed for internal use in [NGN](http://github.com/thinkfirst/NGN).\nHowever; it is capable of providing the same features for Node.JS scripts\nindependently of NGN.\n\nFor alternative versions, see [node-windows](http://github.com/coreybutler/node-windows)\nand [node-mac](http://github.com/coreybutler/node-mac)\n\nThis module makes it possible to daemonize Node.js scripts natively (using systemv init.d scripts).\n\nTo start, install node-linux via:\n\n    npm install node-linux\n\nnode-linux has a utility to run Node.js scripts as Linux daemons.\n\nTo create a service with node-linux, prepare a script like:\n\n```js\n  var Service = require('node-linux').Service;\n\n  // Create a new service object\n  var svc = new Service({\n    name:'Hello World',\n    description: 'The nodejs.org example web server.',\n    script: '/path/to/helloworld.js'\n  });\n\n  // Listen for the \"install\" event, which indicates the\n  // process is available as a service.\n  svc.on('install',function(){\n    svc.start();\n  });\n\n  svc.install();\n```\n\nThe code above creates a new `Service` object, providing a pretty name and description.\nThe `script` attribute identifies the Node.js script that should run as a service. Upon running\nthis, the script will be available to the system. By default, node-linux produces systemv init\nscripts, meaning the services can be managed by typing `service myapp start` or `service myapp stop`\n(or `service myapp status` in some cases).\n\n![Windows Mac](https://raw.github.com/coreybutler/node-linux/master/docs/assets/images/startstopstatus.jpg)\n\nThe `Service` object emits the following events:\n\n- _install_ - Fired when the script is installed as a service.\n- _alreadyinstalled_ - Fired if the script is already known to be a service.\n- _invalidinstallation_ - Fired if an installation is detected but missing required files.\n- _uninstall_ - Fired when an uninstallation is complete.\n- _start_ - Fired when the new service is started.\n- _stop_ - Fired when the service is stopped.\n- _error_ - Fired in some instances when an error occurs.\n- _doesnotexist_ - Fired when an attempt to start a non-existent service is detected.\n\nIn the example above, the script listens for the `install` event. Since this event\nis fired when a service installation is complete, it is safe to start the service.\n\nServices created by node-linux are like other services running on Linux.\nThey can be started/stopped using `service myapp start` or `service myapp stop` and\nlogs are available (default is in /var/log).\n\n## Environment Variables\n\nSometimes 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:\n\n```js\n  var svc = new Service({\n    name:'Hello World',\n    description: 'The nodejs.org example web server.',\n    script: '/path/to/helloworld.js',\n    env: {\n      name: \"HOME\",\n      value: process.env[\"USERPROFILE\"] // service is now able to access the user who created its' home directory\n    }\n  });\n```\n\nYou can also supply an array to set multiple environment variables:\n\n```js\n  var svc = new Service({\n    name:'Hello World',\n    description: 'The nodejs.org example web server.',\n    script: '/path/to/helloworld.js',\n    env: [{\n      name: \"HOME\",\n      value: process.env[\"USERPROFILE\"] // service is now able to access the user who created its' home directory\n    },\n    {\n      name: \"TEMP\",\n      value: path.join(process.env[\"USERPROFILE\"],\"/temp\") // use a temp directory in user's home directory\n    }]\n  });\n```\n\n## Setting run as user/group\n\nBy default your node service will run as root:root. You may not want that.\nJust pass the requested user/group values at startup\n\n```js\n  var svc = new Service({\n    name:'Hello World',\n    description: 'The nodejs.org example web server.',\n    script: '/path/to/helloworld.js',\n    user: \"vagrant\",\n    group: \"vagrant\"\n  });\n```\n\n## Cleaning Up: Uninstall a Service\n\nUninstalling a previously created service is syntactically similar to installation.\n\n```js\n  var Service = require('node-linux').Service;\n\n  // Create a new service object\n  var svc = new Service({\n    name:'Hello World',\n    script: require('path').join(__dirname,'helloworld.js')\n  });\n\n  // Listen for the \"uninstall\" event so we know when it's done.\n  svc.on('uninstall',function(){\n    console.log('Uninstall complete.');\n    console.log('The service exists: ',svc.exists());\n  });\n\n  // Uninstall the service.\n  svc.uninstall();\n```\n\nThe uninstall process only removes process-specific files. **It does NOT delete your Node.js script, but it will remove the logs!**\n\n## What Makes node-linux Services Unique?\n\nLots of things!\n\n**Long Running Processes \u0026 Monitoring:**\n\nThere is no built-in service recovery in most Linux environments, and third party products can be fairly\nlimited or not easily configured from code. Therefore, node-linux creates a wrapper around the Node.js script.\nThis wrapper is responsible for restarting a failed service in an intelligent and configurable manner. For example,\nif your script crashes due to an unknown error, node-linux will attempt to restart it. By default,\nthis occurs every second. However; if the script has a fatal flaw that makes it crash repeatedly,\nit adds unnecessary overhead to the system. node-linux handles this by increasing the time interval\nbetween restarts and capping the maximum number of restarts.\n\n**Smarter Restarts That Won't Pummel Your Server:**\n\nUsing the default settings, node-linux adds 25% to the wait interval each time it needs to restart\nthe script. With the default setting (1 second), the first restart attempt occurs after one second.\nThe second occurs after 1.25 seconds. The third after 1.56 seconds (1.25 increased by 25%) and so on.\nBoth the initial wait time and the growth rate are configuration options that can be passed to a new\n`Service`. For example:\n\n```js\n  var svc = new Service({\n    name:'Hello World',\n    description: 'The nodejs.org example web server.',\n    script: '/path/to/helloworld.js'),\n    wait: 2,\n    grow: .5\n  });\n```\n\nIn this example, the wait period will start at 2 seconds and increase by 50%. So, the second attempt\nwould be 3 seconds later while the fourth would be 4.5 seconds later.\n\n**Don't DOS Yourself!**\n\nRepetitive recycling could potentially go on forever with a bad script. To handle these situations, node-linux\nsupports two kinds of caps. Using `maxRetries` will cap the maximum number of restart attempts. By\ndefault, this is unlimited. Setting it to 3 would tell the process to no longer restart a process\nafter it has failed 3 times. Another option is `maxRestarts`, which caps the number of restarts attempted\nwithin 60 seconds. For example, if this is set to 3 (the default) and the process crashes/restarts repeatedly,\nnode-linux will cease restart attempts after the 3rd cycle in a 60 second window. Both of these\nconfiguration options can be set, just like `wait` or `grow`.\n\nFinally, an attribute called `abortOnError` can be set to `true` if you want your script to **not** restart\nat all when it exits with an error.\n\n## How Services Are Made\n\nnode-linux uses the templates to generate init.d scripts for each Node.js script deployed as a\nservice. This file is created in `/etc/init.d` by default. Additionally, a log file is\ngenerated in `/var/log/\u003cname\u003e` for general output and error logging.\n\n_Event Logging_\n\nA log source named `myappname.log` provides basic logging for the service. It can be used to see\nwhen the entire service starts/stops.\n\nBy default, any `console.log`, `console.warn`, `console.error` or other output will be made available\nin one of these two files.\n\n# Contributions\n\nDue to some unforeseen life circumstances, I was not able to add all of the features I'd hoped to add\nbefore releasing this. I'll chip away at them over time, but I would be very interested in community contributions\nin the following areas:\n\n- systemd script generation\n- upstart script generation\n\nI have also added a tag in the issues called `feature request` to keep a running to-do list.\n\nIf you are interested in working on one of these features, please get in touch with me before you start to discuss\nthe feature.\n\n# License (MIT)\n\nCopyright (c) 2013 Corey Butler\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n'Software'), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","funding_links":["https://github.com/sponsors/coreybutler"],"categories":["Packages","Repository","包","Process management","目录","Number"],"sub_categories":["Process management","进程管理","流程管理"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoreybutler%2Fnode-linux","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcoreybutler%2Fnode-linux","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoreybutler%2Fnode-linux/lists"}