Adding your own systemd service in Linux
You will learn how to add a service in Linux. You will learn how to add a service through the systemd daemon using the systemctl command, and how to configure the service to start when Linux boots. You will need administrator privileges.
Linux servers are designed to run continuously. To make this possible, they need to handle many complex tasks required for the system to work. Services are used for this purpose, allowing specific system functions to run. Services can run in the background, and properly configured services are started when the system boots. You can add your own services that will run tasks in the background.
Adding a service
Go to the directory used for service configuration files.
cd /etc/systemd/systemCreate a configuration file for your service with a name ending in .service, for example:
sudo nano <service_name>.serviceAdd the following content to the file and adjust it to your needs:
[Unit]
Description=<service_description>
[Service]
User=<user e.g. root>
WorkingDirectory=<script_directory e.g. /root>
ExecStart=<absolute_script_path>
Restart=always
[Install]
WantedBy=multi-user.targetSave the file and exit the editor. Reload the service files so that the new service is picked up.
sudo systemctl daemon-reloadThen start the service.
sudo systemctl start <service_name>.serviceTo check the status of your service, enter:
sudo systemctl status <service_name>.serviceTo enable the service so that it starts after every reboot, enter:
sudo systemctl enable <service_name>.serviceTo disable the service so that it does not start after every reboot, enter:
sudo systemctl disable <service_name>.serviceComments (0)
No comments yet.
Add a comment
Comments are published after moderation. Your e-mail address stays private.