Systemd

Create a systemd service

Posted by

We will create a systemd service, we will start an executable file and keep it alive. Create a file /etc/systemd/system/myservice.service and put inside.

[Unit]
Description=My service
After=network.target
StartLimitIntervalSec=0

[Service]
Type=simple
Restart=always
RestartSec=1
User=myuser
ExecStart=/path/to/my/exec/file

[Install]
WantedBy=multi-user.target

If your exec file is a script, you need to add the interpreter, modify the ExecStart line.

ExecStart=/bin/bash /path/to/my/exec/script.sh

Now we will make it executable, activate it on startup and start it.

chmod 755 /etc/systemd/system/myservice.service
systemctl enable myservice
systemctl start myservice

We can check that our service is runnig

[root]$ systemctl status myservice
● myservice.service - My Service
   Loaded: loaded (/etc/systemd/system/myservice.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2020-04-24 19:38:09 UTC; 3min 0s ago
 Main PID: 23404 (bash)
    Tasks: 49 (limit: 1143)
   CGroup: /system.slice/myservice.service
           ├─23404 /bin/bash /path/to/my/exec/script.sh
           └─23405 ./binary

Apr 24 19:38:09 server systemd[1]: Started binary service.

Leave a Reply

Your email address will not be published. Required fields are marked *