Raspberry Pi; Working with systemctl
Working with systemctl
on a Raspberry Pi is similar to working with any other Linux system that uses the systemd
init system. systemctl
is the command-line interface for interacting with systemd
, allowing you to manage services, check their status, enable or disable them, and more. Below are some common tasks and commands you’ll find useful when working with systemctl
on a Raspberry Pi.
Common systemctl
Commands
1. Check the Status of a Service
To check the status of a specific service, use:sudo systemctl status myservice.service
Replace myservice.service
with the name of the service you want to check.
2. Start a Service
To start a service immediately, use:sudo systemctl start myservice.service
3. Stop a Service
To stop a running service, use:sudo systemctl stop myservice.service
4. Restart a Service
To restart a service, which stops it and then starts it again, use:sudo systemctl restart myservice.service
5. Enable a Service at Boot
To enable a service to start automatically at boot, use:sudo systemctl enable myservice.service
6. Disable a Service from Starting at Boot
To prevent a service from starting automatically at boot, use:sudo systemctl disable myservice.service
7. List All Services
To list all services and their statuses, use:systemctl list-units --type=service
8. List Enabled Services
To see only the services that are enabled to start at boot, use:systemctl list-unit-files --type=service --state=enabled
9. View Logs for a Service
To view logs related to a specific service, usejournalctl
:
sudo journalctl -u myservice.service
Note: Use your arrow keys up [ 🠉 ] and down [ 🠋 ] to navigate. Use [ Q ] to leave.
-r
to reverse the output:
sudo journalctl -u myservice.service -r
--follow
to view logs in real-time:
sudo journalctl -u myservice.service --follow
10. Check System Boot Logs
To view logs related to the system boot process, you can use:journalctl -b
Managing Services on Raspberry Pi
Creating a Custom Service
If you need to create your own service (like a Python script), follow these steps:
1. Create the Service File:sudo nano /etc/systemd/system/mycustom.service
[Unit]
Description=My Custom Service
After=network.target
[Service]
ExecStart=/usr/bin/python3 /home/pi/myscript.py
WorkingDirectory=/home/pi
StandardOutput=inherit
StandardError=inherit
Restart=always
[Install]
WantedBy=multi-user.target
Adjust the ExecStart
line to point to your script and interpreter.
sudo systemctl daemon-reload
sudo systemctl start mycustom.service
sudo systemctl enable mycustom.service
Note: Skip the systemctl enable
command, if you don’t want to run your script every time the pi boots.
Summary of Important Commands
- Start a Service:
sudo systemctl start myservice.service
- Stop a Service:
sudo systemctl stop myservice.service
- Restart a Service:
sudo systemctl restart myservice.service
- Enable a Service at Boot:
sudo systemctl enable myservice.service
- Disable a Service from Boot:
sudo systemctl disable myservice.service
- Check Service Status:
sudo systemctl status myservice.service
- View Logs:
sudo journalctl -u myservice.service
Additional Tips
- Use
sudo
for commands that require administrative privileges. - Always check the syntax of your service files carefully; errors can prevent the service from starting.
- Use
systemctl list-dependencies
to see what services are dependent on others, which can help in troubleshooting.