Linux
OS-Mgmt
Systemd

Introduction

systemd is a system and service manager for Linux operating systems. It is used to manage services, processes, and system states. This guide covers creating, managing, and troubleshooting custom systemd services.

Updating Systemd After Service Creation

After creating or modifying a systemd service unit file, always reload the systemd daemon to apply changes:

sudo systemctl daemon-reload

Creating a Systemd Service

  1. Service Unit File Path

    Service unit files are typically located in /etc/systemd/system/ for custom services and /lib/systemd/system/ for official services. To create a new service, use the following path:

    vim /etc/systemd/system/"cmd-name".service
  2. Script Path

    Place your service script in a standard executable location:

    vim /usr/sbin/script-name.sh

Example Service Unit Files

Custom Apache2 Service

To create a custom systemd service for Apache2:

sudo nano /etc/systemd/system/customname.service

Service unit file content:

[Unit]
Description=Apache2 Web Server
After=network.target
 
[Service]
Type=simple
ExecStart=/usr/sbin/apachectl start
ExecStop=/usr/sbin/apachectl stop
ExecReload=/usr/sbin/apachectl graceful
KillMode=process
Restart=always
 
[Install]
WantedBy=multi-user.target

Official Apache2 Service

To view the official Apache2 service unit file:

sudo vim /lib/systemd/system/apache2.service

Official service unit file content:

[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=https://httpd.apache.org/docs/2.4/
 
[Service]
Type=forking
Environment=APACHE_STARTED_BY_SYSTEMD=true
ExecStart=/usr/sbin/apachectl start
ExecStop=/usr/sbin/apachectl graceful-stop
ExecReload=/usr/sbin/apachectl graceful
KillMode=mixed
PrivateTmp=true
Restart=on-abort
 
[Install]
WantedBy=multi-user.target

Running a Script

To create a service that executes a script:

sudo vim /etc/systemd/system/example-service.service

Service unit file content:

[Unit]
Description=Example systemd service.
 
[Service]
Type=simple
ExecStart=/bin/bash /usr/bin/test_service.sh
 
[Install]
WantedBy=multi-user.target

Running a Script Every 30 Seconds

To create a service that runs a script every 30 seconds:

sudo vim /etc/systemd/system/nmap-service.service

Service unit file content:

[Unit]
Description=Demonstration of custom nmap service.
After=network.target
 
[Service]
Type=simple
User=root
ExecStart=/usr/bin/nmap -sS -O -oN /home/<user>/results.txt localhost
Restart=always
RestartSec=30
 
[Install]
WantedBy=multi-user.target

Gunicorn Service for a Test Application

To create a service for running Gunicorn:

sudo vim /etc/systemd/system/gunicorn-test-app.service

Service unit file content:

[Unit]
Description=Gunicorn daemon for serving test-app
After=network.target
 
[Service]
User=root
Group=root
WorkingDirectory=/apps/test-app/
Environment="PATH=/apps/test-app/bin"
ExecStart=/apps/test-app/bin/gunicorn --workers 9 -t 0 --bind 127.0.0.1:5001 -m 007 wsgi:app --log-level debug --access-logfile /var/log/gunicorn/test_app_access.log --error-logfile /var/log/gunicorn/test_app_error.log
ExecReload=/bin/kill -s HUP $MAINPID
RestartSec=5
 
[Install]
WantedBy=multi-user.target

Conclusion

Systemd is a powerful tool for managing system services and processes. By creating and configuring service unit files, you can control how services start, stop, and behave. Always remember to reload the systemd daemon after making changes to service files to ensure your configurations take effect.

This guide covers essential aspects of creating and managing systemd services, including paths for service unit files, sample configurations, and instructions for updating systemd after changes.


🧙 AI Wizard - Instant Page Insights

Click the button below to analyze this page.
Get an AI-generated summary and key insights in seconds.
Powered by Perplexity AI!