Systemd guideline - KeegMitch/Operations-Engineering-group-c GitHub Wiki
How to create a systemd service
- Create a bash script if needed for your service first:
#!/bin/bash
while $(sleep 30);
do
echo "Hello, systemd world"
done
- 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
-
Copy this file to /etc/systemd/system using this command:
sudo cp example.service /etc/systemd/system -
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:
The "static" state means that it's missing an [Install] section in the file, so that means you cannot automatically enable or disable it.
- Start the service
systemctl start hello.service
- Verify that the service works
systemctl status hello.service
Your output should look like this:
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