services systemd (unit)
Source: linuxhandbook
Sending output to a file π
Source: https://unix.stackexchange.com/questions/245037/saving-process-output-to-a-file-in-systemd-unit-file
Commands are not subject to shell parsing eg β< > << >> and &β are not treated specially. Use /bin/sh 'script.sh > out'
to get around this.
File location π
If running as root user π
File should be saved at etc/systemd/system/
sudo nano /etc/systemd/system/SERVICE_NAME.service
If running as non-root user π
File should be saved at ~/.config/systemd/user/
. Note that This folder usually wonβt exist so create it.
mkdir -p ~/.config/systemd/user
nano ~/.config/systemd/user/SERVICE_NAME.service
Example Contents π
This is the same for both. See the linuxhandbook for more info on what should be set in the file. You can find a list of targets here.
[Unit]
Description=User Facing name of service
After=network.target
[Service]
ExecStart=/bin/bash -c '/usr/bin/my_awesome_program >> /home/user/out.txt 2>&1'
Type=simple
Restart=always
[Install]
WantedBy=default.target
Enabling and starting the service π
If running as root user π
sudo systemctl daemon-reload
sudo systemctl enable SERVICE_NAME.service
sudo systemctl start SERVICE_NAME
If running as non-root user π
systemctl --user daemon-reload
systemctl --user enable SERVICE_NAME.service
systemctl --user start SERVICE_NAME
Updates to service config π
This section applies if youβve made changes to the serviceβs configuration and want to apply those changes
If running as root user π
sudo systemctl daemon-reload
sudo systemctl restart SERVICE_NAME
If running as non-root user π
systemctl --user daemon-reload
systemctl --user restart SERVICE_NAME
Removing Services π
Source: https://www.baeldung.com/linux/create-remove-systemd-services#1-removing-custom-systemd-services
If running as root user π
sudo systemctl stop SERVICE_NAME
sudo systemctl disable SERVICE_NAME
sudo rm /etc/systemd/system/SERVICE_NAME.service
sudo systemctl daemon-reload
If running as non-root user π
systemctl --user stop SERVICE_NAME
systemctl --user disable SERVICE_NAME
rm ~/.config/systemd/user/SERVICE_NAME.service
systemctl --user daemon-reload