linux:manage-systemd-services

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
linux:manage-systemd-services [2017/09/01 15:15] michaellinux:manage-systemd-services [2019/03/07 12:50] (current) – [Systemctl Examples] michael
Line 1: Line 1:
-====== Manage Systend Services ======+====== Manage Systemd Services ======
 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.
 +
 +{{:linux:systemctl.png?nolink&500|}}
 +
 +**//Um eigene Systemd Unite Files zu erstellen bitte hier schauen://** -> <btn type="danger" modal="modal-systemd">Systemd Service konfigurieren</btn>
 + 
 +<modal id="modal-systemd" size="lg" remote="linux:systemd"></modal>
 +
  
 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.+''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.+''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/server chronyd.service - NTP client/server
    Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled)    Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled)
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.
  
-<sxh bash; gutter: false;> +<sxh plain; gutter: false;> 
-[[email protected] ~]# systemctl is-active chronyd+[root@admin-server ~]# systemctl is-active chronyd
 active active
 </sxh> </sxh>
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’.
  
-<sxh bash; gutter: false;> +<sxh plain; gutter: false;> 
-[[email protected] ~]# systemctl is-enabled chronyd+[root@admin-server ~]# systemctl is-enabled chronyd
 enabled enabled
 </sxh> </sxh>
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.+''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.
  
-<sxh bash; gutter: false;> +<sxh plain; gutter: false;> 
-[[email protected] ~]# systemctl stop chronyd +[root@admin-server ~]# systemctl stop chronyd 
-[[email protected] ~]# systemctl is-active chronyd+[root@admin-server ~]# systemctl is-active chronyd
 inactive inactive
 </sxh> </sxh>
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.
  
-<sxh bash; gutter: false;> +<sxh plain; gutter: false;> 
-[[email protected] ~]# systemctl start chronyd +[root@admin-server ~]# systemctl start chronyd 
-[[email protected] ~]# systemctl is-active chronyd+[root@admin-server ~]# systemctl is-active chronyd
 active active
 </sxh> </sxh>
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.
  
-<sxh bash; gutter: false;> +<sxh plain; gutter: false;> 
-[[email protected] ~]# systemctl restart chronyd+[root@admin-server ~]# systemctl restart chronyd
 </sxh> </sxh>
  
 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.
  
-<sxh bash; gutter: false;> +<sxh plain; gutter: false;> 
-[[email protected] ~]# systemctl is-active chronyd+[root@admin-server ~]# systemctl is-active chronyd
 active active
-[[email protected] ~]# systemctl stop chronyd + 
-[[email protected] ~]# systemctl is-active chronyd+[root@admin-server ~]# systemctl stop chronyd 
 +[root@admin-server ~]# systemctl is-active chronyd
 inactive inactive
-[[email protected] ~]# systemctl try-restart chronyd + 
-[[email protected] ~]# systemctl is-active chronyd+[root@admin-server ~]# systemctl try-restart chronyd 
 +[root@admin-server ~]# systemctl is-active chronyd
 inactive inactive
-[[email protected] ~]# systemctl restart chronyd + 
-[[email protected] ~]# systemctl is-active chronyd+[root@admin-server ~]# systemctl restart chronyd 
 +[root@admin-server ~]# systemctl is-active chronyd
 active active
 </sxh> </sxh>
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.
  
-<sxh bash; gutter: false;> +<sxh plain; gutter: false;> 
-[[email protected] ~]# systemctl reload sshd+[root@admin-server ~]# systemctl reload sshd
 </sxh> </sxh>
  
Line 100: Line 111:
 ==== 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.+''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.
  
-<sxh bash; gutter: false;> +<sxh plain; gutter: false;> 
-[[email protected] ~]# systemctl is-enabled chronyd+[root@admin-server ~]# systemctl is-enabled chronyd
 enabled enabled
-[[email protected] ~]# systemctl disable chronyd+ 
 +[root@admin-server ~]# systemctl disable chronyd
 rm '/etc/systemd/system/multi-user.target.wants/chronyd.service' rm '/etc/systemd/system/multi-user.target.wants/chronyd.service'
-[[email protected] ~]# systemctl is-enabled chronyd+[root@admin-server ~]# systemctl is-enabled chronyd
 disabled disabled
-[[email protected] ~]# systemctl is-active chronyd+ 
 +[root@admin-server ~]# systemctl is-active chronyd
 active active
 </sxh> </sxh>
Line 117: Line 130:
 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.
  
-<sxh bash; gutter: false;> +<sxh plain; gutter: false;> 
-[[email protected] ~]# systemctl enable chronyd+[root@admin-server ~]# systemctl enable chronyd
 ln -s '/usr/lib/systemd/system/chronyd.service' '/etc/systemd/system/multi-user.target.wants/chronyd.service' ln -s '/usr/lib/systemd/system/chronyd.service' '/etc/systemd/system/multi-user.target.wants/chronyd.service'
 </sxh> </sxh>
Line 124: Line 137:
 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.
  
-<sxh bash; gutter: false;> +<sxh plain; gutter: false;> 
-[[email protected] ~]# systemctl enable chronyd +[root@admin-server ~]# systemctl enable chronyd 
-[[email protected] ~]# systemctl reenable chronyd+[root@admin-server ~]# systemctl reenable chronyd
 rm '/etc/systemd/system/multi-user.target.wants/chronyd.service' rm '/etc/systemd/system/multi-user.target.wants/chronyd.service'
 ln -s '/usr/lib/systemd/system/chronyd.service' '/etc/systemd/system/multi-user.target.wants/chronyd.service' ln -s '/usr/lib/systemd/system/chronyd.service' '/etc/systemd/system/multi-user.target.wants/chronyd.service'
Line 133: Line 146:
 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.
  
-<sxh bash; gutter: false;> +<sxh plain; gutter: false;> 
-[[email protected] ~]# systemctl mask chronyd+[root@admin-server ~]# systemctl mask chronyd
 ln -s '/dev/null' '/etc/systemd/system/chronyd.service' ln -s '/dev/null' '/etc/systemd/system/chronyd.service'
 </sxh> </sxh>
Line 140: Line 153:
 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.
  
-<sxh bash; gutter: false;> +<sxh plain; gutter: false;> 
-[[email protected] ~]# systemctl is-active chronyd+[root@admin-server ~]# systemctl is-active chronyd
 active active
-[[email protected] ~]# systemctl stop chronyd + 
-[[email protected] ~]# systemctl start chronyd+[root@admin-server ~]# systemctl stop chronyd 
 +[root@admin-server ~]# systemctl start chronyd
 Failed to issue method call: Unit chronyd.service is masked. Failed to issue method call: Unit chronyd.service is masked.
 </sxh> </sxh>
Line 150: Line 164:
 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.
  
-<sxh bash; gutter: false;> +<sxh plain; gutter: false;> 
-[[email protected] ~]# systemctl status chronyd+[root@admin-server ~]# systemctl status chronyd
 chronyd.service chronyd.service
    Loaded: masked (/dev/null)    Loaded: masked (/dev/null)
    Active: inactive (dead)    Active: inactive (dead)
-[[email protected] ~]# systemctl is-active chronyd+    
 +[root@admin-server ~]# systemctl is-active chronyd
 inactive inactive
 </sxh> </sxh>
Line 161: Line 176:
 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.
  
-<sxh bash; gutter: false;> +<sxh plain; gutter: false;> 
-[[email protected] ~]# systemctl unmask chronyd+[root@admin-server ~]# systemctl unmask chronyd
 rm '/etc/systemd/system/chronyd.service' rm '/etc/systemd/system/chronyd.service'
-[[email protected] ~]# systemctl start chronyd + 
-[[email protected] ~]# systemctl is-active chronyd+[root@admin-server ~]# systemctl start chronyd 
 +[root@admin-server ~]# systemctl is-active chronyd
 active active
 </sxh> </sxh>
Line 176: Line 192:
 ==== View Status of all Services ==== ==== 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.+''A list of the current status of all services can be viewed with the command below, remove --all to only list active services.''
  
-<sxh bash; gutter: false;>+<sxh plain; gutter: false;>
 systemctl list-units --type service --all systemctl list-units --type service --all
 </sxh> </sxh>
  
-A list of all services can be viewed to see if they are currently enabled with the command below.+''A list of all services can be viewed to see if they are currently enabled with the command below.''
  
-<sxh bash; gutter: false;>+<sxh plain; gutter: false;>
 systemctl list-unit-files --type service systemctl list-unit-files --type service
 </sxh> </sxh>
Line 191: Line 207:
 </WRAP> </WRAP>
  
- 
----- 
  
 ===== Summary ===== ===== 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.+<wrap hi>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.</wrap>
  
 As systemd gains popularity and replaces older alternatives, it is more important to understand how to work with services with systemctl. As systemd gains popularity and replaces older alternatives, it is more important to understand how to work with services with systemctl.
  
- 
- 
----- 
- 
-https://www.rootusers.com/how-to-manage-linux-systemd-services-with-systemctl/ 
  • linux/manage-systemd-services.1504271729.txt.gz
  • Last modified: 2017/09/01 15:15
  • by michael