Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
linux:manage-systemd-services [2017/09/01 15:11] – created michael | linux:manage-systemd-services [2019/03/07 12:50] (current) – [Systemctl Examples] michael | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Manage | + | ====== Manage |
In many Linux based operating systems such as Debian 8, Red Hat Enterprise Linux (RHEL) and CentOS 7 systemd is now the default init system and is used for service management. | In many Linux based operating systems such as Debian 8, Red Hat Enterprise Linux (RHEL) and CentOS 7 systemd is now the default init system and is used for service management. | ||
+ | |||
+ | {{: | ||
+ | |||
+ | **//Um eigene Systemd Unite Files zu erstellen bitte hier schauen:// | ||
+ | |||
+ | <modal id=" | ||
+ | |||
Here we will cover service management with the systemctl command, which is used to control the state of the systemd system and service manager. | Here we will cover service management with the systemctl command, which is used to control the state of the systemd system and service manager. | ||
Line 9: | Line 16: | ||
- | Here are some examples outlining how to use the systemctl command to manage various services. | + | '' |
<WRAP center box 100%> | <WRAP center box 100%> | ||
==== Check the Status of a Service ==== | ==== Check the Status of a Service ==== | ||
- | The current status of a service can be checked as shown below. | + | '' |
+ | |||
+ | <sxh plain; gutter: false;> | ||
+ | [root@admin-server ~]# systemctl status chronyd.service | ||
- | <sxh bash; gutter: false;> | ||
- | [[email protected] ~]# systemctl status chronyd.service | ||
chronyd.service - NTP client/ | chronyd.service - NTP client/ | ||
| | ||
Line 30: | Line 38: | ||
Alternatively we can also check if a service is active by using ‘is-active’ which will show if the service is currently running or not. | Alternatively we can also check if a service is active by using ‘is-active’ which will show if the service is currently running or not. | ||
- | < | + | < |
- | [[email protected] | + | [root@admin-server |
active | active | ||
</ | </ | ||
Line 37: | Line 45: | ||
Similarly we can also check if a service is enabled to start on boot by using ‘is-enabled’. | Similarly we can also check if a service is enabled to start on boot by using ‘is-enabled’. | ||
- | < | + | < |
- | [[email protected] | + | [root@admin-server |
enabled | enabled | ||
</ | </ | ||
Line 48: | Line 56: | ||
==== Starting, Stopping and Restarting Services ==== | ==== Starting, Stopping and Restarting Services ==== | ||
- | Systemctl can be used to start, stop and restart services as demonstrated. | + | '' |
Here we stop the chronyd service and confirm that it is no longer actively running. | Here we stop the chronyd service and confirm that it is no longer actively running. | ||
- | < | + | < |
- | [[email protected] | + | [root@admin-server |
- | [[email protected] | + | [root@admin-server |
inactive | inactive | ||
</ | </ | ||
Line 60: | Line 68: | ||
We can start the service back up and confirm that it is active once again. | We can start the service back up and confirm that it is active once again. | ||
- | < | + | < |
- | [[email protected] | + | [root@admin-server |
- | [[email protected] | + | [root@admin-server |
active | active | ||
</ | </ | ||
Line 68: | Line 76: | ||
Rather than perform the stop then start in two steps, we can instead restart the service. If you check the status of the service after this, the active since time will have changed as the service was restarted and the PID will also change. | Rather than perform the stop then start in two steps, we can instead restart the service. If you check the status of the service after this, the active since time will have changed as the service was restarted and the PID will also change. | ||
- | < | + | < |
- | [[email protected] | + | [root@admin-server |
</ | </ | ||
If you attempt to restart a service that is not currently actively running, it will start up. Instead we can use ‘try-restart’ to only perform a restart if the service is already currently running. If the service is not already running and you use ‘try-restart’ it will not start up. | If you attempt to restart a service that is not currently actively running, it will start up. Instead we can use ‘try-restart’ to only perform a restart if the service is already currently running. If the service is not already running and you use ‘try-restart’ it will not start up. | ||
- | < | + | < |
- | [[email protected] | + | [root@admin-server |
active | active | ||
- | [[email protected] | + | |
- | [[email protected] | + | [root@admin-server |
+ | [root@admin-server | ||
inactive | inactive | ||
- | [[email protected] | + | |
- | [[email protected] | + | [root@admin-server |
+ | [root@admin-server | ||
inactive | inactive | ||
- | [[email protected] | + | |
- | [[email protected] | + | [root@admin-server |
+ | [root@admin-server | ||
active | active | ||
</ | </ | ||
Line 90: | Line 101: | ||
A service can be reloaded which will just refresh things like configuration file changes, the main process will continue to run however so the PID will not change. | A service can be reloaded which will just refresh things like configuration file changes, the main process will continue to run however so the PID will not change. | ||
- | < | + | < |
- | [[email protected] | + | [root@admin-server |
</ | </ | ||
Line 97: | Line 108: | ||
+ | <WRAP center box 100%> | ||
==== Enabling and Disabling Services ==== | ==== Enabling and Disabling Services ==== | ||
- | + | '' | |
- | If a service is enabled it will be started automatically during system boot, however if a service is disabled it will not automatically start up during system boot. It is possible for a user or another service to manually start up the disabled service. | + | |
Below we can see that the chronyd service is enabled, after disabling it the symlink is removed. Once disabled the service will still be actively running, however if the system is rebooted the service will not start up unless manually started. | Below we can see that the chronyd service is enabled, after disabling it the symlink is removed. Once disabled the service will still be actively running, however if the system is rebooted the service will not start up unless manually started. | ||
- | [[email protected] | + | <sxh plain; gutter: false;> |
+ | [root@admin-server | ||
enabled | enabled | ||
- | [[email protected] | + | |
+ | [root@admin-server | ||
rm '/ | rm '/ | ||
- | [[email protected] | + | [root@admin-server |
disabled | disabled | ||
- | [[email protected] | + | |
+ | [root@admin-server | ||
active | active | ||
+ | </ | ||
+ | |||
The symlink will be recreated when the service is enabled to start up during system boot. | The symlink will be recreated when the service is enabled to start up during system boot. | ||
- | [[email protected] | + | <sxh plain; gutter: false;> |
+ | [root@admin-server | ||
ln -s '/ | ln -s '/ | ||
+ | </ | ||
+ | |||
If you try to enable a service that is already enabled, it will not recreate the symlink which already exists so there will be no output. We can reset the symlink using ‘reenable’ which will first delete the symlink and then recreate it. | If you try to enable a service that is already enabled, it will not recreate the symlink which already exists so there will be no output. We can reset the symlink using ‘reenable’ which will first delete the symlink and then recreate it. | ||
- | [[email protected] | + | <sxh plain; gutter: false;> |
- | [[email protected] | + | [root@admin-server |
+ | [root@admin-server | ||
rm '/ | rm '/ | ||
ln -s '/ | ln -s '/ | ||
+ | </ | ||
+ | |||
To prevent a service from being started automatically and manually by the user or other services we can mask the service. Masking the service essentially directs the symlink to /dev/null so that when it is used, nothing happens. | To prevent a service from being started automatically and manually by the user or other services we can mask the service. Masking the service essentially directs the symlink to /dev/null so that when it is used, nothing happens. | ||
- | [[email protected] | + | <sxh plain; gutter: false;> |
+ | [root@admin-server | ||
ln -s '/ | ln -s '/ | ||
+ | </ | ||
+ | |||
Once the service is masked it will not start on boot and can not be manually started. If a running process is masked it will remain active. | Once the service is masked it will not start on boot and can not be manually started. If a running process is masked it will remain active. | ||
- | [[email protected] | + | <sxh plain; gutter: false;> |
+ | [root@admin-server | ||
active | active | ||
- | [[email protected] | + | |
- | [[email protected] | + | [root@admin-server |
+ | [root@admin-server | ||
Failed to issue method call: Unit chronyd.service is masked. | Failed to issue method call: Unit chronyd.service is masked. | ||
+ | </ | ||
+ | |||
To demonstrate this, the test server has been rebooted while the service is masked and a status on the service has been performed after the server had booted back up, confirming that it is not running. | To demonstrate this, the test server has been rebooted while the service is masked and a status on the service has been performed after the server had booted back up, confirming that it is not running. | ||
- | [[email protected] | + | <sxh plain; gutter: false;> |
+ | [root@admin-server | ||
chronyd.service | chronyd.service | ||
| | ||
| | ||
- | [[email protected] | + | |
+ | [root@admin-server | ||
inactive | inactive | ||
+ | </ | ||
To reverse this the service can be unmasked, it can then be started up successfully. | To reverse this the service can be unmasked, it can then be started up successfully. | ||
- | [[email protected] | + | <sxh plain; gutter: false;> |
+ | [root@admin-server | ||
rm '/ | rm '/ | ||
- | [[email protected] | + | |
- | [[email protected] | + | [root@admin-server |
+ | [root@admin-server | ||
active | active | ||
- | View Status of all Services | + | </ |
- | A list of the current status of all services can be viewed with the command below, remove --all to only list active services. | + | </ |
+ | |||
+ | |||
+ | <WRAP center box 100%> | ||
+ | ==== View Status of all Services ==== | ||
+ | |||
+ | '' | ||
+ | |||
+ | <sxh plain; gutter: false;> | ||
systemctl list-units --type service --all | systemctl list-units --type service --all | ||
- | A list of all services can be viewed to see if they are currently enabled with the command below. | + | </ |
+ | '' | ||
+ | |||
+ | <sxh plain; gutter: false;> | ||
systemctl list-unit-files --type service | systemctl list-unit-files --type service | ||
+ | </ | ||
+ | |||
There are many more things that systemctl can do, for further information check out the manual page by entering ‘man systemctl’ at the command line. | There are many more things that systemctl can do, for further information check out the manual page by entering ‘man systemctl’ at the command line. | ||
+ | </ | ||
- | Summary | ||
- | With systemctl we can check if a service is currently active or enabled to start up automatically during system boot. We can start, stop, restart and reload services, as well as disable them from starting up during system boot and even mask them to prevent them being started up completely. | + | ===== Summary ===== |
- | As systemd gains popularity and replaces older alternatives, | + | <wrap hi>With systemctl we can check if a service |
+ | As systemd gains popularity and replaces older alternatives, | ||
- | |||
- | ---- | ||
- | |||
- | https:// |