{"id":5088,"date":"2023-05-11T00:25:10","date_gmt":"2023-05-11T05:25:10","guid":{"rendered":"https:\/\/www.incredigeek.com\/home\/?p=5088"},"modified":"2023-05-11T00:25:10","modified_gmt":"2023-05-11T05:25:10","slug":"running-node-app-as-systemd-service","status":"publish","type":"post","link":"https:\/\/www.incredigeek.com\/home\/running-node-app-as-systemd-service\/","title":{"rendered":"Running Node App as systemd Service"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In this post we will be using systemd to run a node application.  This is helpful as it will automatically start the app when the server starts so we don&#8217;t have to manually.  These steps can easily be modified to run a bash script, or any other application.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create systemd file<\/li>\n\n\n\n<li>Customize systemd file<\/li>\n\n\n\n<li>Enable systemd file<\/li>\n<\/ul>\n\n\n\n<p>We&#8217;ll be creating a service for the <a href=\"https:\/\/www.incredigeek.com\/home\/an-https-version-of-simple-whisper-web-interface\/\" data-type=\"post\" data-id=\"5079\">Simple Whisper Web Interface<\/a> as an example.  Chang things as needed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create systemd file<\/h2>\n\n\n\n<p>This is super simple.  We create a .service file in \/lib\/systemd\/system.  When we enable the service, it will create a symlink to this file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo vim \/lib\/systemd\/system\/whisperweb.service<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Customize systemd file<\/h2>\n\n\n\n<p>Change the settings as appropriate.  It would be a good idea to run any service as a limited user that only has the rights needed to get the job done.  Do note that you will need to have any prerequisites installed and available for that user to use.  I.e. libraries installed with npm etc.  <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[Unit]\nDescription=Simple Whisper Web Interface Service File\nAfter=network.target\n\n[Service]\nType=simple\nUser=whisperuser\nExecStart=\/usr\/bin\/node mainssl.js\nWorkingDirectory=\/home\/whisperuser\/\nRestart=on-failure\n\n[Install]\nWantedBy=multi-user.target<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Enable systemd file<\/h2>\n\n\n\n<p>Enabling the service will create a symlink that will then run this service file on system boot.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl enable whisperweb.service<\/pre>\n\n\n\n<p>And now we can start the service.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl start whisperweb.service<\/pre>\n\n\n\n<p>We can verify that the service is running by running<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl status whisperweb.service<\/pre>\n\n\n\n<p>The following article has some great explanations on what different options in the unit file mean and do.<\/p>\n\n\n\n<p><a href=\"https:\/\/nodesource.com\/blog\/running-your-node-js-app-with-systemd-part-1\/\">https:\/\/nodesource.com\/blog\/running-your-node-js-app-with-systemd-part-1\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post we will be using systemd to run a node application. This is helpful as it will automatically start the app when the server starts so we don&#8217;t have to manually. These steps can easily be modified to &hellip; <a href=\"https:\/\/www.incredigeek.com\/home\/running-node-app-as-systemd-service\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[339,7,1471,1436,1495,1494,1493],"class_list":["post-5088","post","type-post","status-publish","format-standard","hentry","category-linux","tag-boot","tag-linux-2","tag-node","tag-nodejs","tag-start-up","tag-systemd","tag-unit-file"],"_links":{"self":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5088","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/comments?post=5088"}],"version-history":[{"count":1,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5088\/revisions"}],"predecessor-version":[{"id":5090,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5088\/revisions\/5090"}],"wp:attachment":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/media?parent=5088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/categories?post=5088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/tags?post=5088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}