Skip to main content

Systemd configurations

Simple instance deployment

Systemd Service for TCP

Create a service

data-listener.service

[Unit]
Description=Data Listener for ClientX
After=network.target

[Service]
User=data-listener
WorkingDirectory=/opt/data-listener/data-listener
ExecStart=/opt/data-listener/data-listener
Restart=on-failure

[Install]
WantedBy=default.target

Enable and start the socket and service

    sudo systemctl daemon-reload
sudo systemctl enable data-listener.service
sudo systemctl start data-listener.service

Systemd Service for Unix Sockets

If you are listening on socket and want the service to be activated on socket activity.

Create the service data-listener.service

[Unit]
Description=Data Listener for CLIENT
After=network.target
Requires=data-listener.socket

[Service]
User=data-listener
WorkingDirectory=/opt/data-listener/data-listener
ExecStart=/opt/data-listener/data-listener
Restart=on-failure

[Install]
WantedBy=default.target

Create data-listener-CLIENT.socket


[Unit]
Description=Data Listener Server Accept Sockets for CLIENT NAME

[Socket]
# AF_UNIX domain socket
# SocketUser, SocketGroup, etc. may be needed for Unix domain sockets
SocketUser=data-listener
SocketGroup=data-listener
ListenStream=/opt/data-listener/sockets/CLIENT.sock

Backlog=8192

[Install]
WantedBy=sockets.target

Enable and start the socket and service

    sudo systemctl daemon-reload
sudo systemctl enable data-listener.socket
sudo systemctl enable data-listener.service
sudo systemctl start data-listener.socket
sudo systemctl start data-listener.service

Verify that the services are running

    sudo systemctl status data-listener.socket
sudo systemctl status data-listener.service

With Templates

With one Systemd service, you can manage and run multiple instances of data listener.

Deployment Structure

Flexible deployment supporting multiople clients and protocols here

Let's imagine that we want to receive Pointgrab notifications on different ports.

By using %I and %i we can provide a template and later activate data-listener instance for each client like that:

systemctl enable pointgrab@CLIENT.service
systemctl start pointgrab@CLIENT.service

create a pointgrab@.service template like that in /etc/systemd/system

[Unit]
Description=Pointgrab Listener for %I
Documentation=https://data-listener.gudasoft.com/
After=network.target
AssertPathExists=/opt/pointgrab/%I/current
ConditionPathIsReadWrite=|/opt/pointgrab/%I/shared/data
ConditionPathIsReadWrite=|/opt/pointgrab/%I/shared/log

[Service]
Type=simple

# Restart=always
Restart=on-failure
RestartSec=3

StartLimitBurst=5
# StartLimitIntervalSec=10

User=deployment
EnvironmentFile=/opt/pointgrab/%I/current/env/%I.toml
WorkingDirectory=/opt/pointgrab/%I/current

ExecStart=/usr/bin/env /opt/pointgrab/%I/current/data-listener

[Install]
WantedBy=multi-user.target

Rotation

rotate-pointgrab@.service

[Unit]
Description=Rotate Files for %I (%i)
Documentation=https://data-listener.gudasoft.com/
After=network.target

[Service]
Type=simple
Restart=no

User=root

Environment=RACK_ENV=production
WorkingDirectory=/opt/pointgrab/%i/current

ExecStartPre=/bin/systemctl stop pointgrab@%i.service
ExecStartPre=/bin/sleep 0.5

ExecStart=/usr/bin/env bundle exec --keep-file-descriptors bundle exec something_to_rotate
ExecStartPost=/bin/sleep 0.5
ExecStartPost=/bin/systemctl start pointgrab@%i.service

[Install]
WantedBy=multi-user.target

rotate-pointgrab@.timer

[Unit]
Description=Trigger the rotations of the %i pointgrab files

[Timer]
OnCalendar=*:0/59
Persistent=true
RandomizedDelaySec=5m

[Install]
WantedBy=timers.target

Startup

    systemctl daemon-reload
systemctl start pointgrab@CLIENT.service
journalctl -u pointgrab@CLIENT.service

systemctl status pointgrab@CLIENT.service
systemctl start pointgrab@CLIENT.service
systemd-analyze verify pointgrab@CLIENT.service

systemctl enable rotate-pointgrab@CLIENT.timer
systemctl start rotate-pointgrab@CLIENT.timer

systemctl show-environment
systemctl set-environment bird=coco
systemctl unset-environment bird

Debug the timers

    systemctl list-timers --all

if a timer gets out of sync, it may help to delete its stamp-* file in /var/lib/systemd/timers (or ~/.local/share/systemd/ in case of user timers). These are zero length files which mark the last time each timer was run. If deleted, they will be reconstructed on the next start of their timer.

    systemctl daemon-reload
systemctl enable rotate-pointgrab@CLIENT.timer
systemctl start rotate-pointgrab@CLIENT.timer

Debug the rotation

    systemctl daemon-reload
systemctl stop rotate-pointgrab@CLIENT.service
systemctl disable rotate-pointgrab@CLIENT.service

systemctl start rotate-pointgrab@CLIENT.service
systemctl status rotate-pointgrab@CLIENT.service

systemctl enable rotate-pointgrab@CLIENT.service
journalctl -f -u rotate-pointgrab@CLIENT.service