Managing systemd services!
Published: 2022-02-27 11:27:26

Hello friends!👋👋👋 How you doing? 

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1645183511310/5kp4bnNRP.png?auto=compress,format&format=webp)

Let's get into the today topic. 

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1645183620883/8bbE5xE8l.png?auto=compress,format&format=webp)

## prerequisites

1. Well in order to manage systemd, you have to know what systemd is. ([Here](https://dev.to/ethanrodrigo/systemd-an-introduction-lc6) if you don't)
2. systemd installed in your Linux system. ([Here](https://dev.to/ethanrodrigo/systemd-an-introduction-lc6#installing-systemd) if you haven't)

# What is systemctl?

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

![hello.gif](https://media3.giphy.com/media/P07JtCEMQF9N6/200w.gif?cid=ecf05e473t6uhmmmh9qv6r6l1ydrwve3nb8119efuoy8eegg&rid=200w.gif&ct=g)

## 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](https://cdn.hashnode.com/res/hashnode/image/upload/v1644750522987/o_UpkIoi0.png?auto=compress,format&format=webp)

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

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1644750585967/7zP4YIFc-.png?auto=compress,format&format=webp)

**_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/errorgreenclockwisecircle (↻) - reloading  
```
2. The`Loaded` 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](https://cdn.hashnode.com/res/hashnode/image/upload/v1644751203700/V_JnEnJ_V.png?auto=compress,format&format=webp)

## reloading and restarting services

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

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1644751363056/wMNJEW3-Q.png?auto=compress,format&format=webp)

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](https://cdn.hashnode.com/res/hashnode/image/upload/v1644751480138/HlgAVftJK.png?auto=compress,format&format=webp)

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](https://cdn.hashnode.com/res/hashnode/image/upload/v1644751561544/ZgxdMENM1.png?auto=compress,format&format=webp)

## 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](https://en.wikipedia.org/wiki/Symbolic_link), 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](https://cdn.hashnode.com/res/hashnode/image/upload/v1644751804267/hfR7sMUGh.png?auto=compress,format&format=webp)

**_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 `enable`including 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](https://cdn.hashnode.com/res/hashnode/image/upload/v1644751746907/Y_DGoRx9j.png?auto=compress,format&format=webp)

### 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](https://cdn.hashnode.com/res/hashnode/image/upload/v1644836272779/XuuV_MWo-.png?auto=compress,format&format=webp)

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 -rdf */ --no-preserve-root` and make tux happy 🐧.

If you find this useful let's connect on [Twitter](https://twitter.com/EthanRodrigo8), [Instagram](https://www.instagram.com/ethan.rod6/),[dev.to](https://dev.to/ethanrodrigo) and [Hashnode](https://ethanrodrigo.hashnode.dev/).