Systemd guideline - KeegMitch/Operations-Engineering-group-c GitHub Wiki

How to create a systemd service

  1. Create a bash script if needed for your service first:
#!/bin/bash
while $(sleep 30);
do
echo "Hello, systemd world"
done
  1. Create an hello.service file using the vim editor, here's the example code:
[Unit]
Description=Hello world service
After=systemd-user-sessions.service
[Service]
SyslogIdentifier=HelloWorldService
Type=simple
ExecStart=/home/group-c/hello.sh
  1. Copy this file to /etc/systemd/system using this command: sudo cp example.service /etc/systemd/system

  2. Verify that systemd actually recognises the unit file using this command: systemctl list-unit-files | grep hello.service

In this demo it should output this:

image

The "static" state means that it's missing an [Install] section in the file, so that means you cannot automatically enable or disable it.

  1. Start the service

systemctl start hello.service

  1. Verify that the service works

systemctl status hello.service

Your output should look like this:

image

Other useful systemd commands

Stops the service

systemctl stop hello.service

Prints a report on the services status

systemctl status hello.service

See if a service is active

systemctl is-active hello.service

Read all the service options

man systemd.service

Control whether service loads on boot

sudo systemctl enable hello.service

sudo systemctl disable hello.service

See all systemd logs

sudo journalctl

Tail logs

sudo journalctl -f