Raspberry Pi: Start, stop, restart, enable and disable services

The default Linux distribution is Raspbian. If you run a Raspberry Pi not as a desktop replacement, but rather as a server, then you have to do it on the command line with services, services and daemons.
The word “daemon” derives from the “D” isk “a” nd “Execution” mon stor and refers to utilities that start at system startup or later and work in the background, so a daemon is a service or service.
Services are controlled by special commands. There is also the option to start services automatically at system startup and to switch this functionality off again. There are also commands that give information about a service. For example, whether it is running or automatically started.
Here we take as an example the service SSH (ssh), which usually runs and where you can try the different commands. Of course only if you are not logged in to Raspberry Pi via SSH.
These commands have to be differentiated between Raspbian Wheezy and Raspbian Jessie. There was a change of the init system from “Sys-V-Init” to “systemd”. The old commands of Raspbian Wheezy also usually work under Raspbian Jessie, but not the other way around. Under “systemd” you should already get used to the new commands.
task
- Manually start, stop and restart services with Sys-V-Init (Raspbian Wheezy)
- Manually start, stop and restart services with systemd (Raspbian Jessie)
- Start or disable services automatically at boot time (rcconf).
Solution: Manually start, stop and restart services with Sys-V-Init (Raspbian Wheezy)
Query status of a service (without root privileges):
service ssh status
Get detailed status about the service (with root privileges):
sudo service ssh status
Start service:
sudo service ssh start
Stop running service:
sudo service ssh stop
NOTE: Stopping a service in the runlevels does not permanently remove a service. On the contrary, a service that is stopped but not removed is just waiting to be started. This can be done while updating the package. Or just a restart.
To actually disable a service is to disable the service in the runlevels or to remove the corresponding package via apt-get remove.
Restart running service (disconnects existing connections with the service):
sudo service ssh restart
Reload the configuration (existing connections will NOT be disconnected):
sudo service ssh reload
Reload the configuration while disconnecting existing connections:
sudo service ssh force reload
Raspbian Wheezy uses update-rc.d to enable and disable services in each runlevel.
As a rule, services are automatically started during installation and entered in the correct runlevel. However, it may be that the service has been temporarily decommissioned and now wants to reactivate it.
If you want to have a deactivated service automatically started at boot time, you can create the links to the start / stop script with the following command.
Service should start automatically in the future:
sudo update-rc.d ssh defaults
Service should NOT start automatically in the future. The links to the start / stop script are deleted from the runlevels.
sudo update-rc.d -f ssh remove
Solution: Start services manually, stop and restart with “systemd” (from Raspbian Jessie)
When changing from Raspbian Wheezy to Raspbian Jessie, there was a change of the init system from “Sys-V-Init” to “systemd”. The old commands of Raspbian Wheezy also usually work under Raspbian Jessie, but not the other way around. Under “systemd” you should already get used to the new commands.
Show status of all systemd units:
systemctl
Systemd units can end in .service, .socket, .target, .timer, .mount, .automount, .device or .path. If no unit type is appended, the default type is “.service”.
systemctl status UNIT.service
Example with the service SSH:
systemctl status ssh.service
Show status to the service:
systemctl status ssh
Show more detailed status about the service:
sudo systemctl status ssh
Show very detailed status for the service:
sudo systemctl show ssh
Start service:
sudo systemctl start ssh
Stop running service:
sudo systemctl stop ssh
Stop a service IMMEDIATELY:
sudo systemctl kill ssh
Service should start automatically in the future:
sudo systemctl enable ssh
Service should NOT start automatically anymore in the future:
sudo systemctl disable ssh
Indicates whether the service starts automatically:
systemctl is-enabled ssh
Show systemd unit:
systemctl cat ssh.service
Meaning of the service status
SSH is started and starts automatically
Loaded: loaded (/lib/systemd/system/ssh.service; enabled)
Active: active (running) ...
SSH is stopped and starts automatically
Loaded: loaded (/lib/systemd/system/ssh.service; enabled)
Active: inactive (dead)
SSH is started, but does not start automatically
Loaded: loaded (/lib/systemd/system/ssh.service; disabled)
Active: active (running) ...
SSH is stopped and will not start automatically
Loaded: loaded (/lib/systemd/system/ssh.service; disabled)
Active: inactive (dead)
Hints
On a freshly installed Linux distribution, especially Raspbian, run not really useless services. So you should not shut down any services that later turn out to be important and only lead to the system being unusable. Before deactivating, you should be aware of exactly what the deactivation could have and whether it really makes sense.
Example: How to permanently remove a service
Using the example of the swap service, which can be easily removed and also restored, without putting a running system at risk, the removal of a service can be tracked.
Leave a Reply