{"id":21399645,"url":"https://github.com/natancabral/run-nodejs-on-service-with-systemd-on-linux","last_synced_at":"2025-06-10T12:04:43.818Z","repository":{"id":114484155,"uuid":"381458651","full_name":"natancabral/run-nodejs-on-service-with-systemd-on-linux","owner":"natancabral","description":"Node.js as a running service is becoming more and more popular these days. One of the issues many developers face is how to ensure their node.js service starts automatically, and more importantly how to keep it running should it crash.","archived":false,"fork":false,"pushed_at":"2021-07-02T14:07:45.000Z","size":90,"stargazers_count":17,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-16T01:53:25.657Z","etag":null,"topics":["linux","node","nodejs","service","systemctl","systemd","unix"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/natancabral.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2021-06-29T18:18:34.000Z","updated_at":"2025-02-09T22:03:18.000Z","dependencies_parsed_at":"2023-05-16T22:45:26.151Z","dependency_job_id":null,"html_url":"https://github.com/natancabral/run-nodejs-on-service-with-systemd-on-linux","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/natancabral%2Frun-nodejs-on-service-with-systemd-on-linux","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/natancabral%2Frun-nodejs-on-service-with-systemd-on-linux/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/natancabral%2Frun-nodejs-on-service-with-systemd-on-linux/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/natancabral%2Frun-nodejs-on-service-with-systemd-on-linux/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/natancabral","download_url":"https://codeload.github.com/natancabral/run-nodejs-on-service-with-systemd-on-linux/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/natancabral%2Frun-nodejs-on-service-with-systemd-on-linux/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259072698,"owners_count":22801069,"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":["linux","node","nodejs","service","systemctl","systemd","unix"],"created_at":"2024-11-22T15:15:54.847Z","updated_at":"2025-06-10T12:04:43.782Z","avatar_url":"https://github.com/natancabral.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Run node.js service with systemd on Linux\n\n\u003cimg src=\"assets/external-content.duckduckgo.com.gif\"/\u003e\n\n**Node.js** as a running service is becoming more and more popular these days. One of the issues many developers face is how to ensure their node.js service starts automatically, and more importantly how to keep it running should it crash. \n\n**Using systemd, which makes this process a lot simpler and more efficient**, and means that we do not need another scripts to run your **server node.js**.\n\n[read my article](https://natancabral.medium.com/run-node-js-service-with-systemd-on-linux-42cfdf0ad7b2)\n\n## Create the node.js server\n```js\nconst http = require('http');\nconst hostname = '0.0.0.0'; // listen on all ports\nconst port = 3311;\n\nhttp.createServer((req, res) =\u003e {\n\n  res.writeHead(200, { 'Content-Type': 'text/plain' });\n  res.end('Hello World');\n\n}).listen(port, hostname, () =\u003e {\n\n  console.log(`Server running at http://${hostname}:${port}/`);\n\n});\n```\nWe will save this file (for example) as **/home/myserver/server.js** . Verify the node server works on terminal:\n\n```bash\n$ node /home/myserver/server.js\n# or nodejs, maybe\n$ nodejs /home/myserver/server.js\n```\n\nOutput: \n- Server running at http://0.0.0.0:3311/\n\n## Create the service file on Systemd\n\nCreate a service file on \n**/etc/systemd/system/** or \n**/lib/systemd/system/** (etc|lib).\n\n```bash\n$ cd /etc/systemd/system/\n$ nano myserver.service\n```\nFile location: \n- **/etc/systemd/system/myserver.service**\n\n```bash\n[Unit]\nDescription=My Little Server\n# Documentation=https://\n# Author: Natan Cabral\n\n[Service]\n# Start Service and Examples\nExecStart=/usr/local/bin/node /home/myserver/server.js\n# ExecStart=/usr/bin/sudo /usr/bin/node /home/myserver/server.js\n# ExecStart=/usr/local/bin/node /var/www/project/myserver/server.js\n\n# Options Stop and Restart\n# ExecStop=\n# ExecReload=\n\n# Required on some systems\n# WorkingDirectory=/home/myserver/\n# WorkingDirectory=/var/www/myproject/\n\n# Restart service after 10 seconds if node service crashes\nRestartSec=10\nRestart=always\n# Restart=on-failure\n\n# Output to syslog\nStandardOutput=syslog\nStandardError=syslog\nSyslogIdentifier=nodejs-my-server-example\n\n# #### please, not root users\n# RHEL/Fedora uses 'nobody'\n# User=nouser\n# Debian/Ubuntu uses 'nogroup', RHEL/Fedora uses 'nobody'\n# Group=nogroup\n\n# Variables\nEnvironment=PATH=/usr/bin:/usr/local/bin\n# Environment=NODE_ENV=production\n# Environment=NODE_PORT=3001\n# Environment=\"SECRET=pGNqduRFkB4K9C2vijOmUDa2kPtUhArN\"\n# Environment=\"ANOTHER_SECRET=JP8YLOc2bsNlrGuD6LVTq7L36obpjzxd\"\n\n[Install]\nWantedBy=multi-user.target\n```\n\n## Enable the service\n\n#### Enable the service on boot\n```bash\n$ systemctl enable myserver.service\n```\n#### Start the service\n```bash\n$ systemctl start myserver.service\n$ systemctl restart myserver.service\n$ systemctl status myserver.service\n```\n#### Verify it is running\n```bash\n$ journalctl -u myserver.service\n```\n#### Reload all services if you make changes to the service\n```bash\n$ systemctl daemon-reload\n$ systemctl restart myserver.service\n```\n#### List process\n```bash\n$ ps -ef | grep myserver.js\n```\n#### Maybe we need permissions\n```bash\n$ chmod +x /etc/systemd/system/myserver.service\n$ chmod 664 /etc/systemd/system/myserver.service\n```\n[about permissions](https://www.systemconf.com/2020/12/25/using-chmod-x-command-on-linux-and-unix-with-examples/)\n\n#### To kill process if you need try\n```bash\n$ kill -9 {numberPID}\n```\n[read my article](https://natancabral.medium.com/run-node-js-service-with-systemd-on-linux-42cfdf0ad7b2)\n\nToDo\n\n- Dev NPX - to create services files and save. prompt and readline.\n\nNames to remember\n\n- **systemctl** - system control\n- **systemd** - system manager standard\n- **ps** - process\n- **chmod** - change the mod\n\n## Author\n\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\n      \u003cimg src=\"https://github.com/natancabral.png?s=100\" width=\"100\"/\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      Natan Cabral\u003cbr /\u003e\n      \u003ca href=\"mailto:natancabral@hotmail.com\"\u003enatancabral@hotmail.com\u003c/a\u003e\u003cbr /\u003e\n      \u003ca href=\"https://github.com/natancabral/\"\u003ehttps://github.com/natancabral/\u003c/a\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnatancabral%2Frun-nodejs-on-service-with-systemd-on-linux","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnatancabral%2Frun-nodejs-on-service-with-systemd-on-linux","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnatancabral%2Frun-nodejs-on-service-with-systemd-on-linux/lists"}