Zet - How do I create a systemd service?
How do I create a systemd service?
Creating a systemd Service (Example: Homebox Service)
1. Create a systemd Service File
Run the following command:
sudo vim /etc/systemd/system/homebox.service
Add the following content:
[Unit]
Description=Homebox Service
After=network.target
[Service]
ExecStart=/home/kev/homebox
WorkingDirectory=/home/kev/
Restart=always
User=kev
Group=kev
[Install]
WantedBy=multi-user.target
Save and exit by pressing Esc, then typing :wq and pressing Enter.
2. Reload systemd and Enable the Service
sudo systemctl daemon-reload
sudo systemctl enable homebox
sudo systemctl start homebox
3. Verify Service Status
sudo systemctl status homebox
4. View Logs
journalctl -u homebox -f
To check logs from a specific time range:
journalctl -u homebox --since "1 hour ago"
Creating Additional Services
To create more services, replace homebox with the name of your application and update the ExecStart and WorkingDirectory paths accordingly.
Example:
sudo vim /etc/systemd/system/myapp.service
Edit the file:
[Unit]
Description=MyApp Service
After=network.target
[Service]
ExecStart=/home/kev/myapp
WorkingDirectory=/home/kev/
Restart=always
User=kev
Group=kev
[Install]
WantedBy=multi-user.target
Then enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable myapp
sudo systemctl start myapp
Stopping and Restarting Services
To stop a service:
sudo systemctl stop servicename
To restart a service:
sudo systemctl restart servicename
To disable a service from starting on boot:
sudo systemctl disable servicename
[!see-more] 20241231125346#How do I manage services using systemctl?
#systemd