<?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: angeloscle</title>
    <description>The latest articles on DEV Community by angeloscle (@angeloscle).</description>
    <link>https://dev.to/angeloscle</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1058978%2Fa7fe7209-141f-4d11-b56f-e7a8340ff31c.jpeg</url>
      <title>DEV Community: angeloscle</title>
      <link>https://dev.to/angeloscle</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/angeloscle"/>
    <language>en</language>
    <item>
      <title>Run a node.js app with systemd</title>
      <dc:creator>angeloscle</dc:creator>
      <pubDate>Sun, 24 Nov 2024 19:04:15 +0000</pubDate>
      <link>https://dev.to/angeloscle/run-nodejs-app-with-systemd-3g6b</link>
      <guid>https://dev.to/angeloscle/run-nodejs-app-with-systemd-3g6b</guid>
      <description>&lt;p&gt;Using systemd to run your Node.js app simplifies and streamlines the process, eliminating the need for additional scripts to manage your application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create the node.js server
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;hostname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;127.0.0.1&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="s1"&gt;; // listen on all ports
const port = 3311;

http.createServer((req, res) =&amp;gt; {

  res.writeHead(200, { &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="nx"&gt;Content&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Type&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;plain&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; });
  res.end(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="nx"&gt;Node&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;running&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;);

}).listen(port, hostname, () =&amp;gt; {

  console.log(`Server running at http://${hostname}:${port}/`);

});
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can save this file under the path /home/node/server.js and make sure you can it run within console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;nodejs /home/myserver/server.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expected result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Server running at http://127.0.0.1:3311/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Create the service file
&lt;/h2&gt;

&lt;p&gt;Create a service file on /etc/systemd/system/&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; /etc/systemd/system/
&lt;span class="nv"&gt;$ &lt;/span&gt;nano node.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;File content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight systemd"&gt;&lt;code&gt;&lt;span class="k"&gt;[Unit]&lt;/span&gt;
&lt;span class="nt"&gt;Description&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;Start Node.js App
&lt;span class="nt"&gt;After&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;network.target

&lt;span class="k"&gt;[Service]&lt;/span&gt;
&lt;span class="nt"&gt;ExecStart&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;/usr/bin/npm start
&lt;span class="nt"&gt;KillMode&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;control-group
&lt;span class="nt"&gt;WorkingDirectory&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;/home/myserver/
&lt;span class="nt"&gt;Restart&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;on-failure

&lt;span class="k"&gt;[Install]&lt;/span&gt;
&lt;span class="nt"&gt;WantedBy&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;multi-user.target
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;A line-by-line explanation of the above file's content:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;[Unit]&lt;/strong&gt;&lt;br&gt;
The &lt;strong&gt;[Unit]&lt;/strong&gt; section provides metadata and dependencies for the unit file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Description=Start Node.js App&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A short description of the service. This will appear in systemctl commands like systemctl status node.service.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;After=network.target&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Specifies that this service should start only after the network.target is active. It ensures the network is up before your Node.js app starts, which is often necessary for apps relying on networking.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;[Service]&lt;/strong&gt;&lt;br&gt;
The &lt;strong&gt;[Service]&lt;/strong&gt; section defines how the service behaves when it starts, stops, or restarts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ExecStart=/usr/bin/npm start&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The command to execute when starting the service. In this case, it runs npm start from the Node.js application's working directory. This assumes your package.json file has a start script configured.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;KillMode=control-group&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Controls how processes in the service's cgroup are terminated. control-group means that systemd will kill all processes in the control group (not just the main process) when stopping the service.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WorkingDirectory=/home/myserver/&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The directory where the service runs. This is typically the root directory of your Node.js application, containing files like package.json.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Restart=on-failure&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Configures the service to automatically restart if it exits with a non-zero exit code (indicating failure). This improves the reliability of the service by ensuring it stays running.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;[Install]&lt;/strong&gt;&lt;br&gt;
The &lt;strong&gt;[Install]&lt;/strong&gt; section specifies how and when the service should be started during the system boot process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WantedBy=multi-user.target&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Indicates that this service should be started in the multi-user.target environment, which is a common system state for non-graphical, multi-user systems. This ensures the service starts automatically when the system boots into this state.&lt;/p&gt;
&lt;h2&gt;
  
  
  Enable the service
&lt;/h2&gt;

&lt;p&gt;Enable the service to start on server boot&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;systemctl &lt;span class="nb"&gt;enable &lt;/span&gt;node.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Start the service
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;systemctl start node.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Verify it is running
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;systemctl status node.service
&lt;span class="nv"&gt;$ &lt;/span&gt;journalctl &lt;span class="nt"&gt;-u&lt;/span&gt; node.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  **Journalctl
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;A tool which interacts with the systemd journal for querying logs&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&amp;amp; a useful journalctl command&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;journalctl &lt;span class="nt"&gt;-xfeu&lt;/span&gt; node.service &lt;span class="nt"&gt;--since&lt;/span&gt; &lt;span class="s2"&gt;"2024-11-13 00:00:00"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;--since "2024-11-13 00:00:00"&lt;/strong&gt;&lt;br&gt;
With the above &lt;em&gt;option&lt;/em&gt; of the command you can limit the logs to entries that occurred after the specified date and time (2024-11-13 00:00:00). This ensures you only see logs from a specific time frame onward.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;-f:&lt;/strong&gt; Follows the log in real time. Similar to tail -f, this allows you to see new log entries as they are written&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;-e&lt;/strong&gt;: Jumps to the end of the logs, showing the most recent entries. This is useful when combined with -f&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;-u node.service&lt;/strong&gt;: Filters logs for a specific systemd service (node.service in this case). Only log entries related to node.service will be shown.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://roomieverse.net" rel="noopener noreferrer"&gt;https://roomieverse.net&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
