Managing systemd Services!

Managing systemd Services!

Β·

5 min read

Hello friends!πŸ‘‹πŸ‘‹πŸ‘‹ How you doing?

image.png

I'm doubting whether the cover picture goes with the topic.πŸ™„Let me know in the comments.

Anyhow let's get into the today topic.

image.png

prerequisites

  1. Well in order to manage systemd, you have to know what systemd is. (Here if you don't)
  2. systemd installed in your Linux system. (Here if you haven't)

What is systemctl?

The systemctl is a utility to introspect and control the systemd system and services.

hello.gif

What is a unit?

In systemd a unit, is any resource that the system knows how to manage and operate on. This is the principal object that the systemd tools know how to address. Units can control hardware, services, sockets, etc. These units are defined in a configuration file called a unit.

Note: Though only the services have been mentioned later in the article, it also referrs to the other units types such as sockets, timers, etc. (More on types would be in another article).

Managing services with systemctl

checking the status of a service

The status of a service can be checked with systemctl's status flag. It provides information whether it’s active, running, failed, and if it’s failed, the reason for failure.

The pure systemctl status command gives you the status of the system.

image.png

If you want the status of a specific service, use systemctl status serviceName.serivce or just serviceName

image.png

output explained

  1. dot(●) before the name of the service. systemctl uses this dot with specific color scheme to show the unit status at a glance.

     white circle (β—‹) - inactive/maintenance
    
     green dot(🟒) - active 
    
     white dotβšͺ️ - deactivating
    
     read cross(❌) - failed/error 
    
     green clockwise circle (↻) - reloading
    
  2. TheLoaded line shows whether the unit has been loaded into memory or not. It also provides the path to the .service file of the unit. Then you have the state of the service enabled or disabled in the same line.

  3. The Active line shows the active state, i.e. active or inactive. active could also mean started, plugged in, etc depending on the unit type. The unit could also be in process of changing the state with activating or deactivating. It would be failed state if the service is failed in some way such as a crash, exiting with an error code or timeout.

In addition to that you’ll get the documentation, memory usage, main PID, etc with the status command.

PS: except Loaded and Active the output could be changed from service to service.

starting and stopping service

If you want to stop(deactivate) or start(activate) a service you can use systemctl stop and systemctl start respectively. Once you execute the command, you need to enter the password to authenticate the user and if it’s success it’ll splashes a message ==== AUTHENTICATION COMPLETE ====.

image.png

reloading and restarting services

A running service can be restarted using systemctl restart serviceName, instead of stopping and starting it manually.

image.png

Yet, if you just wanna add some changes to the service you can do systemctl reload instead of reastart. It will reload the service-specific configurations.

image.png

However if you are confused which one to use you can use systemctl reload-or-restart serviceName or systemctl try-reload-or-restart serviceName. The only different is reload-or-restart starts units that are not running, whilst try-reload-or-restart does nothing to the not running units.

image.png

enabling and disabling services

systemd enable

Starting services manually on every boot would be tedious. That’s why enable is here to help you.

The systemctl enable takes a unit file or the path to a unit file as arguments.

Enabling a service creates a set of symlinks, as encoded in [Install] section of the unit file (More on unit files would be on another article). Once the symlinks have been created, the system manager configuration is reloaded, in order to take the action immediately.

enable tells the system manager to automatically starts a service on boot or a particular hardware is plugged in. You need to reboot the system in order to take the effect into action, or else use --now flag, in order to enable it without rebooting.

image.png

Remember;

The enable and start are orthogonal, i.e units can be enabled without being started or started without being enabled. Enabling simply hooks the unit into various suggested places (for example, so that the unit is automatically started on boot or when a particular kind of hardware is plugged in). Starting actually spawns the daemon process (in case of service units), or binds the socket (in case of socket units), and so on.

Depending on whether --system, --user, --runtime, or --global is specified, systemd enables the unit for the system, for the calling user only, for only this boot of the system, or for all future logins of all users. Note that in the last case, no systemd daemon configuration is reloaded.

systemd disable

disable on the other hand removes all the symlinks created by the enableincluding manually created ones. In addition to that disable only accepts units names and not the path of the unit.

The --system, --user, --runtime, or --global are same as enable.

image.png

reenable

In addition to that we have reenable , which disable and enable the unit.

Here we are disabling and enabling again the cgconfig. image.png

Note how reenable is same as enable and disable. As in with disable it removes the sysmlink anb then it creates it again into the path which is often /usr/lib/systemd/system.

Conclusion

With systemctl you can manage systemd services. But what if you want to create a service? Can we create a service for systemd? Let's find it out on next article. Till then bye... bye...

Thank you for reading! 😊😊 Now go and execute sudo rm -rf /* --no-preserve-root and make tux happy 🐧.

If you find this useful let's connect on Twitter, Instagram,dev.to and Hashnode.

Β