Systemd intro
Lately, Linux distributions seem to have standardized on systemd to start/stop services.
It does provide a lot more such as:
- sudo capabilities
- cron capabilities
basic commands
Below is a list of basic commands
-- | Action | syntax | Example |
---|---|---|---|
-- | Start a service | systemctl start [servicename] | systemctl start apache2 |
-- | Stop a service | systemctl stop [servicename]] | systemctl stop postfix |
-- | restart a service | systemctl restart [servicename]]] | systemctl restart postfix |
-- | List services | systemctl list-units | systemctl list-units |
-- | Enable a service | systemctl enable [servicename] | systemectl enable apache2 |
-- | Disable a service | systemctl disable [servicename] | systemectl disable apache2 |
Sample service
I created a script to simplify runnning a live youtube radio using ffmpeg available here.
it worked well but to be more reliable, I added a systemd service to start the script on bootup and easy start/stop
The service is below:
[Unit]
Description=Simple Live Stream
After=multi-user.target sound.target network-online.target
[Service]
WorkingDirectory=/usr/local/stream
User=ubuntu
ExecStart=/usr/local/stream/stream.sh -c /usr/local/stream/conf/stream.conf
KillMode=control-group
Restart=on-failure
TimeoutSec=1
[Install]
WantedBy=multi-user.target
Alias=streaming.service
Notable script features
- uses the After directive to wait for multi user mode and being online
- uses ExecStart to run a command called /usr/local/stream.sh
- defines an alias from stream to streaming
- in the Service section, it defines ubuntu as the running user
- in the service section, it defines the working directory as /usr/local/stream