Systemd – How it starts Your system

29/12/2020
Chưa phân loại
Systemd was developed when developers found that starting the system in parallel was not possible. The older systems goes through their system startup files one by one. This can cause the boot to stop when waiting for resources that are slow. One example of this is when a process needs to have the network running. Using the network is very slow, other resources should not have to wait. This problem can be mitigated in other ways but with systemd, all startup is made in parallel where possible. The result should be a much faster startup for complex systems, which includes your desktop system.

Systemd: Father of all other processes

A Linux, and any other *nix, system always starts a process that is the father of all other processes. When you use Systemd, that process is systemd, the process is always called ‘init’ irrespective of init system. Once systemd is running, it will start the daemons that run the subsystems. The daemons control what resources you have and what systems are open. Your login prompt is a service managed by systemd. Systemd starts many other services and it also mounts all your disks, including the snap mounts.

How do you control what starts?

First of all, the earliest init system had runlevels that many readers will recognise. Runlevel 1 was single user mode, 2 multi-user mode without networking and 3-5 multi-user and graphical. Setting the runlevel to 0 meant halt and 6 meant reboot. Developers of systemd found these levels to be unclear so changed it to be called targets. Targets are not exactly the same but has a similar function. When a specific target is running, a specific collection of units are running. More on units later.

Setting the “runlevel”, called a target.

You can change the target while running, for example you could use a terminal to set your target to multi-user. This would stop your GUI, X or Wayland but let you and others, log in with a text console. Use the following command.

$ systemctl isolate multiuser.target

Or, if you want to start your desktop, use the following command instead.

$ systemctl isolate graphical.target

What you are really interested in is how to start the correct level, you can find out what you have right now this way.

$ systemctl get-default

To change the default set it.

$ systemctl set-default graphical.target

This command actually makes a lot of units and services start as you boot your computer. You may want to start or stop a single service though.

STARTING, stopping, enabling and disabling a service.

This is where you have a chance to change what goes on at boot and during normal operation. In contrast to the targets, units are special for a particular service. To stop a service you use ‘systemctl stop’, equally you can use ‘start’ and ‘restart’. To make a service start at boot you use ‘enable’, to stop it ‘disable’. Pick a service and start and stop it while it is running.

$ systemctl status cups.service

This command shows the status of the service, it also shows a list of actions taken by the system regarding the service. To stop the service, use the command below.

$ systemctl stop cups.service

The service is now stopped for your current session. To change it to not starting at the next boot use disable.

$ systemctl disable cups.service

Notice that you could only disable the service, this would have no effect on your current situation. The service just keeps on running. At the next boot though, it will not start the service.

Systemd also controls mounts, devices, sockets and more. Units are lower down and easier to understand, if you have dealt with a Linux before. You can also set these yourself but the most effect on boot is to control services.

How do you add your own scripts?

You can add your own scripts quite easily, the details that are interesting to know is how you make your script or service to depend on others. This is neatly demonstrated in this wifi-resume service file.

[Unit]
Description=Restart networkmanager at resume
After=suspend.target
After=hibernate.target
After=hybrid-sleep.target
 
[Service]
Type=oneshot
ExecStart=/bin/systemctl restart network-manager.service
 
[Install]
WantedBy=suspend.target
WantedBy=hibernate.target
WantedBy=hybrid-sleep.target

This code creates a service that happens once (oneshot) after the three targets mentioned in the code. The code that is executed is in this case a single command. You can just as well point ExecStart to a script of your choosing.

Conclusion

Changing your system, especially how it boots, you can do yourself but beware that this has many consequences for each service so make sure that your code is stable and quick to execute.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Bash shuf command

Shuf is one of those commands that most bash programmers have not heard. For those who have, the experience itself is often...
29/12/2020

Yarock – An Elegant Desktop Music Player for Linux

Yarock is a modern good looking desktop music player for linux, and its packed with great features. It is designed to provide...
28/12/2020

How to install Stellarium 0.16 – A 3D Planetarium Software on Linux

Stellarium 0.16 recently released, is a free open source planetarium app that enables you to have a realistic view of the...
28/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

mua Proxy riêng ở đâu, và nó đem lại lợi ích gì cho người sử dụng
22/11/2022

Hướng dẫn sử dụng Proxy Helper Fakeip khi thuê proxy
21/11/2022

PROXY NUÔI TÀI KHOẢN FACEBOOK – KINH NGHIỆM FAKE IP – THUÊ PROXY GIÁ RẺ
14/11/2022

Mua Proxy Nuôi Zalo Giá Rẻ Tại Onet.com.vn
14/11/2022

BẢNG GIÁ MUA PROXY VIỆT NAM và PROXY US Onet.com.vn
14/11/2022