Service

Linux service configuration guide

Linux service configuration guide

Let’s suppose you want to automatically run script.sh at startup and restart it automatically if it stops.

Make sure that there’s a correct shebang at the first line of script.sh:

#!/bin/bash

Create a .service file under /etc/systemd/system. Name it as desired (e.g., script.service):

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

Edit the file:

[Unit]
Description=Lorem ipsum  # Custom description here

[Service]
ExecStart=/path/to/script.sh
Restart=always  # Restart the script if it exits
RestartSec=5  # Restart after the script exits for 5 seconds
User=user  # Suppose that you want to run it as user

[Install]
WantedBy=multi-user.target

Save and exit, then enable the service:

sudo systemctl enable script.service

Start the service:

sudo systemctl start script.service

Check the status of the service:

sudo systemctl status script.service

References

https://unix.stackexchange.com/questions/472950/systemd-status-203-exec-error-when-creating-new-service (https://unix.stackexchange.com/a/490707)

ChatGPT

Content Licensed under CC BY-SA 4.0. Code licensed under the MIT License.