<?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: Petrus Maas</title>
    <description>The latest articles on DEV Community by Petrus Maas (@petrusjohannesmaas).</description>
    <link>https://dev.to/petrusjohannesmaas</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%2F3562342%2F1b00352a-e7d3-4bf2-9c44-a7569cf596d6.jpeg</url>
      <title>DEV Community: Petrus Maas</title>
      <link>https://dev.to/petrusjohannesmaas</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/petrusjohannesmaas"/>
    <language>en</language>
    <item>
      <title>Creating and managing Systemd services</title>
      <dc:creator>Petrus Maas</dc:creator>
      <pubDate>Mon, 13 Oct 2025 12:10:52 +0000</pubDate>
      <link>https://dev.to/petrusjohannesmaas/creating-and-managing-systemd-services-3nik</link>
      <guid>https://dev.to/petrusjohannesmaas/creating-and-managing-systemd-services-3nik</guid>
      <description>&lt;p&gt;This is a guide on how to create, manage and persist your own systemd service.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But first — what is a system service?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A system service is a background process managed by the operating system.&lt;/li&gt;
&lt;li&gt;It typically handles essential tasks like networking, logging, or scheduling.&lt;/li&gt;
&lt;li&gt;Services are controlled by a service manager — on most Linux systems, that's &lt;code&gt;systemd&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;They can start automatically at boot, on demand, or based on specific triggers.&lt;/li&gt;
&lt;li&gt;You can use system services to &lt;strong&gt;automate tasks&lt;/strong&gt;, such as running scripts at startup or restarting failed processes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;By the end of this guide you'll be able to:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ Create a simple bash script that runs continuously in the background
&lt;/li&gt;
&lt;li&gt;✅ Set up a systemd service to manage and automate that script
&lt;/li&gt;
&lt;li&gt;✅ Configure service behavior, including auto-restart on failure
&lt;/li&gt;
&lt;li&gt;✅ Redirect service output to a log file for monitoring
&lt;/li&gt;
&lt;li&gt;✅ Control the service using &lt;code&gt;systemctl&lt;/code&gt; commands (start, stop, enable, disable, status)
&lt;/li&gt;
&lt;li&gt;✅ Ensure your service starts automatically at boot
&lt;/li&gt;
&lt;li&gt;✅ Use systemd to automate tasks and maintain persistent background processes
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔧 Step by Step Guide
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1. Create the Dummy Script&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Create a file called &lt;code&gt;dummy.sh&lt;/code&gt; and add the following content:&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="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt; | Dummy service is running..."&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; /var/log/dummy-service.log
  &lt;span class="nb"&gt;sleep &lt;/span&gt;10
&lt;span class="k"&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save the file and make it executable:&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="nb"&gt;chmod&lt;/span&gt; +x dummy.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Copy the file to the proper directory:&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="nb"&gt;sudo mv &lt;/span&gt;dummy.sh /usr/local/bin/dummy.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;2. Create the Systemd Service File&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Create a new systemd service file:&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="nb"&gt;sudo &lt;/span&gt;vi /etc/systemd/system/dummy.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ini"&gt;&lt;code&gt;&lt;span class="nn"&gt;[Unit]&lt;/span&gt;
&lt;span class="py"&gt;Description&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;Dummy service&lt;/span&gt;
&lt;span class="py"&gt;After&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;network.target&lt;/span&gt;

&lt;span class="nn"&gt;[Service]&lt;/span&gt;
&lt;span class="py"&gt;ExecStart&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;/usr/local/bin/dummy.sh&lt;/span&gt;
&lt;span class="py"&gt;Restart&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;always&lt;/span&gt;
&lt;span class="py"&gt;User&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;root&lt;/span&gt;
&lt;span class="py"&gt;StandardOutput&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;append:/var/log/dummy-service.log&lt;/span&gt;
&lt;span class="py"&gt;StandardError&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;append:/var/log/dummy-service.log&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Also make sure to set the log file permissions properly to avoid systemd errors:&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="nb"&gt;sudo touch&lt;/span&gt; /var/log/dummy-service.log
&lt;span class="nb"&gt;sudo chmod &lt;/span&gt;644 /var/log/dummy-service.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;3. Reload Systemd and Enable the Service&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Run these commands to apply the changes:&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="nb"&gt;sudo &lt;/span&gt;systemctl daemon-reload
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl &lt;span class="nb"&gt;enable &lt;/span&gt;dummy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;4. Manage the Service&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;You can interact with the service using:&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="nb"&gt;sudo &lt;/span&gt;systemctl start dummy     &lt;span class="c"&gt;# Start the service&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl stop dummy      &lt;span class="c"&gt;# Stop the service&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl &lt;span class="nb"&gt;enable &lt;/span&gt;dummy    &lt;span class="c"&gt;# Enable the service to start on boot&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl disable dummy   &lt;span class="c"&gt;# Disable the service&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl status dummy    &lt;span class="c"&gt;# Check the service status&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;5. Check Logs&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Check your log file:&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="nb"&gt;cat&lt;/span&gt; /var/log/dummy-service.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;6. Ensure the Service Auto-Restarts&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;If the service crashes, &lt;code&gt;Restart=always&lt;/code&gt; ensures it restarts.&lt;/p&gt;




&lt;p&gt;Contact me on socials: &lt;a href="https://www.linkedin.com/in/petrus-maas/" rel="noopener noreferrer"&gt;LINKEDIN&lt;/a&gt;, &lt;a href="https://github.com/petrusjohannesmaas" rel="noopener noreferrer"&gt;GITHUB&lt;/a&gt;, &lt;a href="https://x.com/pjmaasdev" rel="noopener noreferrer"&gt;X&lt;/a&gt;&lt;/p&gt;

</description>
      <category>linux</category>
      <category>tutorial</category>
      <category>automation</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
