<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: terpinmd</title>
    <description>The latest articles on DEV Community by terpinmd (@terpinmd).</description>
    <link>https://dev.to/terpinmd</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F319796%2F01413d6c-480f-4fdb-9032-5baa88dc7e0d.png</url>
      <title>DEV Community: terpinmd</title>
      <link>https://dev.to/terpinmd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/terpinmd"/>
    <language>en</language>
    <item>
      <title>Node app as a service</title>
      <dc:creator>terpinmd</dc:creator>
      <pubDate>Sun, 26 Apr 2020 16:49:00 +0000</pubDate>
      <link>https://dev.to/terpinmd/node-app-as-a-service-5fpf</link>
      <guid>https://dev.to/terpinmd/node-app-as-a-service-5fpf</guid>
      <description>&lt;p&gt;Recently I deployed a node application to a raspberry pi that i had laying around.  There was surprisingly sparse documentation on how to start the app as a service so that when the ssh connection was shut down the app would still run.&lt;/p&gt;

&lt;p&gt;First I wrote a small script and placed in in the pi users home directory:&lt;/p&gt;

&lt;h3&gt;
  
  
  ~/startup (chmod 755)
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;export NODE_OPTIONS="--max-old-space-size=2048"&lt;br&gt;
export PATH=$PATH:/path/to/node/bin (and whatever else you want on path)&lt;br&gt;
npm run prod &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Test the file by executing it:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;sh ~/startup &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;At this point the app was up and running.  Its important to not move on until the startup script is working. &lt;/p&gt;

&lt;p&gt;What I then wanted to do was to be able to run this as a service like:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;sudo service myservice start&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The following is that code which I then placed in /etc/systemd/system/:&lt;/p&gt;

&lt;blockquote&gt;
&lt;h2&gt;
  
  
  /etc/systemd/system/myservice.service
&lt;/h2&gt;

&lt;p&gt;[Unit]&lt;br&gt;
Description=myservice&lt;/p&gt;

&lt;p&gt;[Service]&lt;br&gt;
ExecStart=&lt;strong&gt;/home/pi/startup&lt;/strong&gt;&lt;br&gt;
Restart=on-failure&lt;br&gt;
RestartSec=60s&lt;br&gt;
User=pi&lt;br&gt;
Group=nogroup&lt;br&gt;
WorkingDirectory=&lt;strong&gt;/path/to/app&lt;/strong&gt;&lt;br&gt;
RestartSec=30000ms&lt;/p&gt;

&lt;p&gt;[Install]&lt;br&gt;
WantedBy=multi-user.target&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Many of the above properties are not mandatory.  The key is the ExecStart which references the startup file we created.  Finally, I wanted the service to start on boot which is done via:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;sudo systemctl enable myservice&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The service can be start, stopped and monitored with:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;sudo service myservice start&lt;br&gt;
sudo service myservice stop&lt;br&gt;
sudo service myservice status&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It took me a while to get this to work but once I found the pieces it was very easy. &lt;/p&gt;

</description>
      <category>node</category>
      <category>linux</category>
      <category>pi</category>
    </item>
  </channel>
</rss>
