The Lazy Admin Blog

Home  /  Linux  /  Set up a new systemd service

Set up a new systemd service

May 18, 2024 Linux Leave a Comment

systemd is a modern init system and service manager for Linux, known for its efficient parallelization, robust dependency management, and powerful logging capabilities.
The need to add a new service to systemd can vary, whether you want to ensure a script restarts if it stops or you need to manage dependencies to guarantee the script’s prerequisites are available, systemd provides a straightforward solution for service management.

Here are the general steps for adding a new service to systemd:

  1. Create the unit file:
    touch /etc/systemd/system/YOUR-SERVICE-NAME.service
  2. Define the service with the necessary parameters, here is a basic example:
    [Unit]
    # Description of the service
    Description=My Custom Service
     
    # Ensure this service starts after the network is up
    After=network.target mysql.service
     
     
    [Service]
    # Command to execute when starting the service
    ExecStart=/path/to/your/script.sh
     
    # Restart the service if it stops
    Restart=always
     
    # Run the service as this user
    User=username
     
     
    [Install]
    # Start this service when the system reaches multi-user.target
    WantedBy=multi-user.target

    For additional information, I recommend visiting Red Hat’s documentation on systemd unit files.

  3. Reload systemd:
    sudo systemctl daemon-reload
  4. Enable and start your new service:
    sudo systemctl enable YOUR-SERVICE-NAME
    sudo systemctl start YOUR-SERVICE-NAME

    You can check the status of your script by using:

    systemcrtl status YOUR-SERVICE-NAME

POC:

Let’s say I have a script (/usr/local/bin/poc.sh) that should write “Hello” to a file (/var/check-service) every 5 minutes:

#!/bin/bash
 
# In seconds
timetowait=300
 
while true
do
    echo "`date` - Hello :)" >> /var/check-service
    sleep "$timetowait"
done

And I want to use it as a service.

For the sake of this example, we’ll call the service “poc”, so the unit file should be named poc.service:

noam@noam:~➤ touch /etc/systemd/system/poc.service

And the contents of it would be:

[Unit]
Description=Proof of concept
After=systemd-udevd.service systemd-timesyncd.service
 
[Service]
ExecStart=/usr/local/bin/poc.sh
Restart=always
User=noam
 
[Install]
WantedBy=default.target

Note that the “After” directive specifies that the script should only be loaded after both “systemd-udevd.service” and “systemd-timesyncd.service” have been loaded. This ensures that the script is executed after these services, considering its file writing and time-checking operations.

Then, we’ll reload systemd:

noam@noam:~➤ sudo systemctl daemon-reload

And enable and start the script:

noam@noam:~➤ sudo systemctl enable poc
Created symlink /etc/systemd/system/default.target.wants/poc.service → /etc/systemd/system/poc.service.
noam@noam:~➤ sudo systemctl start poc

Finally, we can check the status of the service:

noam@noam:~➤ sudo systemctl status poc
● poc.service - Proof of concept
Loaded: loaded (/etc/systemd/system/poc.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2024-05-18 22:29:02 IDT; 2s ago
Main PID: 1461092 (poc.sh)
Tasks: 2 (limit: 18721)
Memory: 548.0K
CPU: 3ms
CGroup: /system.slice/poc.service
├─1461092 /bin/bash /usr/local/bin/poc.sh
└─1461094 sleep 300
 
May 18 22:29:02 noam systemd[1]: Started Proof of concept.

And it seems like it works just fine, we can even check the /var/check-service file for changes:

Tags: systemd
Previous Article
Next Article

Leave a Reply

Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Search Our Blog

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
Filter by Categories
Apache
CentOS
CloudLinux
cPanel
Emails
ESXI
iSCSI
JetBackup
Linux
Litespeed
MySQL
NGINX
Oracle
Reduxio
Security
SSL
Uncategorized
VMware
Wordpress
XEN

Tags

apache aspx backup bash CentOS cloudlinux cPanel CXS Emails freetds google htaccess IMAP InnoDB iscsi JetBackup Libmodsecurity litespeed modsec modsecurity mssql MySQL netapp nginx odbc Oracle php php.ini phpselector rsync ssh ssmtp systemd threads VMFS WHM Wordpress xenserver

Popular Posts

  • Convert JetBackup to cPanel structure October 6, 2022
  • How To Install & Configure a Galera Cluster with MariaDB on Centos 7 February 6, 2018
  • Allow a cPanel server to run a VHOST from multiple IP addresses April 3, 2018
  • rsync without prompting for password October 10, 2022

Recent Posts

  • Understanding Why More Threads Can Sometimes Slow Down Performance October 9, 2024
  • Set up a new systemd service May 18, 2024
  • Bash Arrays November 7, 2023
  • rsync without prompting for password October 10, 2022

Recent Comments

  • Sven on rsync without prompting for password
  • TheLazyAdmin on rsync without prompting for password
  • Sven on rsync without prompting for password
  • TheLazyAdmin on Convert JetBackup to cPanel structure
  • Chris on Convert JetBackup to cPanel structure
Privacy Policy • Contact