Linux command tips : systemctl

systemctl

Introduction

This post is the first of a small series about the not-so-new systemd. This post will introduce you, in a very gentle way, to systemd utility :  systemctl.

The systemctl utility comes with options allowing you to start, stop, enable, disable, request status for a given service. Here is a quick overview of the main and mostly used ones.

 

Command information

  • Purpose : Manage system services
  • Required privileges : root
  • Default path (RHel based distros) : /usr/bin/systemctl
  • Used version in this post : systemd 201
  • Exit code : 0=Success, *=Failure

 

The other post from this series

  • How-To : speed up boot process using systemd tools (Still TODO…)

 

 

1) Generalities

systemd is basically a framework which role is to manage and allow parallelism for services and daemons startup on a Linux box bootup. It comes as a replacement to the old “System V” system.

systemd offers the systemctl command to allow one to list, enable, disabled etc… Linux box services. The systemctl is a combination of both the service command AND the chkconfig utility, which means you can control the services behaviour for the current session only (as with the service command) or permanently (as for the chkconfig command).

Both service and chkconfig command still works, but they have been modified to automatically calls the systemctl utility.

 

Just to make everything clear before starting, there is a terminology to be used with systemd, as for any subject, here a sample of the standard terms :

Active : describe the current status of a given service as running.

Inactive : describe the current status of a given service as NOT running.

Enabled : describe the status of a given service at bootup time : it says if the given service is setup to automatically start at boot time, in this case (Enabled) the service will be startup at boot time.

Disabled : describe the status of a given service at bootup time : it says if the given service is setup to automatically start at boot time, in this case (Disabled) the service will NOT be startup at boot time.

Unit : Within systemd context a unit file is often a script used to start/stop/enable/disable a service, it could be seen as the equivalent to the ancient init script (see man systemd.unit for details)

 

 

2) Systemctl services live management

This section is about managing services for the current session, this is the equivalent to the old service command.

 

2.1 Getting services list

 

2.1.1 Getting the list of currently active services

systemctl called without any args would list all active unit, so a simple systemctl should gives you every active services, as :

[root@localhost ~]#systemctl

2.1.1.1 Real life example

Let’s say you want to get the status of a given service, the NTP service for example, you might want to use the following :

[root@localhost ~]# systemctl status ntp.service

But you’d face the following error :

   Loaded: error (Reason: No such file or directory)
   Active: inactive (dead)

This is because the ntp unit service is not named ntp.service, but chronyd.service. In case you get this kind of error i suggest you’d use the following to get more chance in your request :

[root@localhost ~]# systemctl |grep -i ntp
chronyd.service             loaded active running   NTP client/server

Then you can adapt your initial request using the correct service/unit name :

[root@localhost ~]# systemctl status chronyd.service
chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled)
   Active: active (running) since dim. 2013-06-23 10:30:44 CEST; 1h 26min ago
 Main PID: 694 (chronyd)
   CGroup: name=systemd:/system/chronyd.service
           └─694 /usr/sbin/chronyd -u chrony

juin 23 10:30:44 localhost.localdomain chronyd[694]: chronyd version 1.27-git1ca844 starting
juin 23 10:30:44 localhost.localdomain chronyd[694]: Linux kernel major=3 minor=9 patch=6
juin 23 10:30:44 localhost.localdomain chronyd[694]: hz=100 shift_hz=7 freq_scale=1.00000000 nominal_tick=10000 slew_delta_tick=833 max_tick_bias=...ft_pll=2
juin 23 10:30:44 localhost.localdomain chronyd[694]: Frequency 12.586 +/- 21.902 ppm read from /var/lib/chrony/drift
juin 23 10:30:44 localhost.localdomain systemd[1]: Started NTP client/server.
juin 23 10:31:17 localhost.localdomain chronyd[694]: Selected source 88.191.120.99
juin 23 10:32:23 localhost.localdomain chronyd[694]: Selected source 88.190.36.37
juin 23 10:37:47 localhost.localdomain chronyd[694]: Selected source 88.191.120.99
juin 23 11:42:19 localhost.localdomain chronyd[694]: Selected source 109.69.184.210
[root@localhost ~]#

Here we are !

Basically the only step to achieve when using systemd (instead of system V) is to find out the service/unit name.

2.1.2 Getting the list of currently inactive services

I could not find a systemctl native command to list only active services, i used a pipe and grep combination instead. If you know how to do this natively please comment !

systemctl --all |grep inactive

2.1.3 Getting the list of all services (both active and inactive)

Using the –all flag you will see active AND inactive services :

systemctl --all

Sample output (stripped because it just way too long) :

dbus.target                                                       error  inactive dead      dbus.target
emergency.target                                                  loaded inactive dead      Emergency Mode
final.target                                                      loaded inactive dead      Final Step
getty.target                                                      loaded active   active    Login Prompts
rngd.service                                                      loaded failed   failed    Hardware RNG Entropy Gatherer Daemon

 

2.2 Starting or stopping services

The start/stop systemctl commands are used to eventually start or stop any service for the current session, this is the equivalent to the old service <service_name> [start-stop] command. This is not meant to be used to setup a automatic service startup at boot time, for this purpose please check the section 3 : Systemctl services boot time management

2.2.1 Starting services

Starting services with the systemctl command is not more complicated that it used to be with the service command.

The general syntax is :

systemctl start <service_name>.service

Here is an example for the bluetooth service :

systemctl start bluetooth.service

If the command is successful you may not see anything but a new prompt, you can still check the command return code with the usual $? variable, e.g :

systemctl start bluetooth.service && echo $?

You may also want to check the service status using the command shown in next section.

2.2.2 Stopping services

This is similar to starting a service, except that you would pass the stop arg instead of the start arg (!) :

systemctl stop <service_name>.service

 

2.3 Requesting services current status

After you stop, start a service, or if you want to check if the service you wanted to disable (or enable) at startup, you should use the status option, as :

systemctl status bluetooth.service

Sample output :

[root@localhost ~]# systemctl status bluetooth.service
bluetooth.service - Bluetooth service
   Loaded: loaded (/usr/lib/systemd/system/bluetooth.service; enabled)
   Active: inactive (dead)

juin 23 11:00:48 localhost.localdomain bluetoothd[1896]: Bluetooth Management interface initialized
juin 23 11:00:48 localhost.localdomain systemd[1]: Started Bluetooth service.
juin 23 11:00:48 localhost.localdomain bluetoothd[1896]: bluetoothd[1896]: Parsing /etc/bluetooth/input.conf failed: No such file or directory
juin 23 11:00:48 localhost.localdomain bluetoothd[1896]: bluetoothd[1896]: Bluetooth Management interface initialized
juin 23 11:00:51 localhost.localdomain systemd[1]: Started Bluetooth service.
juin 23 11:00:55 localhost.localdomain systemd[1]: Stopping Bluetooth service...
juin 23 11:00:55 localhost.localdomain bluetoothd[1896]: bluetoothd[1896]: Terminating
juin 23 11:00:55 localhost.localdomain bluetoothd[1896]: bluetoothd[1896]: Stopping SDP server
juin 23 11:00:55 localhost.localdomain bluetoothd[1896]: bluetoothd[1896]: Exit
juin 23 11:00:55 localhost.localdomain systemd[1]: Stopped Bluetooth service.
[root@localhost ~]#

 

Output explanation :

The first line (line #2) is a quick description of the requested service (the bluetooth service in that example).

The second line, starting with the “Loaded” keyword, gives you information about the current startup configuration for the given service. It tells you if the service is setup to automatically start on boot or not (the last line word : enabled in this example), it also gives you the path to the init script.

The third line (line #4) gives you the current status of the requested service, the lines starting with “Active” keyword (the service was inactive in this example).

All the other lines are logs excerpt, typically the ones you would find in /var/log/messages.

 

 

3) Systemctl services boot time management

This section is about managing services at boot time, this is the equivalent to the old chkconfig command.

 

3.1 Getting services list

3.1.1 Getting the list of currently enabled services

As for the list of inactive services : i could not find a systemctl native command to achieve this goal, so i used a pipe and grep combination instead. If you know how to do this natively please comment !

[root@localhost ~]# systemctl list-unit-files |grep enabled
cups.path                                 enabled 
abrt-ccpp.service                         enabled 
abrt-oops.service                         enabled 
abrt-uefioops.service                     enabled 
abrt-vmcore.service                       enabled
[...]

3.1.2 Getting the list of currently disabled services

Same story as above, same command :

[root@localhost ~]# systemctl list-unit-files |grep disabled
arp-ethers.service                        disabled
autovt@.service                           disabled
blk-availability.service                  disabled
[...]

3.1.3 Getting the list of currently both enabled AND disabled services

A naked systemctl list-unit-files command will give you every service unit and their respective state :

[root@localhost ~]# systemctl list-unit-files
UNIT FILE                                 STATE   
var-lib-nfs-rpc_pipefs.mount              static  
cups.path                                 enabled 
systemd-ask-password-console.path         static  
abrt-ccpp.service                         enabled 
abrt-oops.service                         enabled 
blk-availability.service                  disabled
bluetooth.service                         disabled

The “static” state means that is started as a dependency to another service, you are not supposed to manually enable or disable this kind of services (see the link under the Resources section for more info).

 

3.2 Enabling or disabling service

The following commands are used to setup a service to be automatically startup (or not) at computer boot, this is the equivalent to the old chkconfig command.

The general syntax is :

systemctl [enable-disable] <service_name>

3.2.1 Enabling a service

Easy, just issue the following command to setup a service at boot time :

[root@localhost ~]# systemctl enable bluetooth
ln -s '/usr/lib/systemd/system/bluetooth.service' '/etc/systemd/system/dbus-org.bluez.service'
ln -s '/usr/lib/systemd/system/bluetooth.service' '/etc/systemd/system/bluetooth.target.wants/bluetooth.service'

See the Starting service section to check if your command worked as expected.

3.2.2 Disabling a service

Just as disabling a service, enter the following to disable a service at boot time :

[root@localhost ~]# systemctl disable bluetooth
rm '/etc/systemd/system/bluetooth.target.wants/bluetooth.service'
rm '/etc/systemd/system/dbus-org.bluez.service'

 

 

Conclusion

This was our first contact to the systemd systemctl utility, i hope this will help you managing your services. We may now try to speed up the boot process, let’s check the next post of this series : TODO !

 

Resources

 

More “Linux command tips” posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site supports SyntaxHighlighter via WP SyntaxHighlighter. It can highlight your code.
How to highlight your code: Paste your code in the comment form, select it and then click the language link button below. This will wrap your code in a <pre> tag and format it when submitted.