Three surprising things you can do with systemd

WBOY
Release: 2023-06-09 15:38:15
forward
1175 people have browsed it

三件可以用 systemd 做的令人惊讶的事情

It's not just about making your computer start up faster.

When systemd first came out, there was a lot of news about how it could speed up boot times. This feature appeals to most people (less so to those who don't reboot), so in many ways, that's the reputation it still has today. While systemd does play a role in starting services in parallel during the boot process, it does much more than that. Here are three things you might not realize systemd can do, but you should take advantage of.

1. Simplify Linux ps

If you have ever used the ​​ps​​, or even just the ​​top​​ command, then You will know that your computer has hundreds of processes running all the time. Sometimes, this is just the information you need to understand what your computer or its users are doing. Other times, what you really need is a general overview.

​systemd-cgtop​​ command provides a simple view of the computer load based on the control group cgroup task schedule. ​​Control groups​​ are important to modern Linux and are basically the underlying support structure for containers and Kubernetes (which is why cloud computing can scale), but they are also a useful structure on a home computer. For example, from the output of ​​systemd-cgtop​​ you can see the load of user processes, not system processes:

Control Group Proc+ %CPU MemoryInput/s Output/s
/ 1835.0 1.6G 0B 3.0M
user.slice42.8 1.1G 0B 174.7K
user.slice/user-1000.slice42.8 968.2M 0B 174.7K
system.slice 652.2 1.5G 0B 2.8M
Copy after login

You can also just view your userspace Processes, or look at user space processes and kernel threads.

This is by no means a replacement for ​​top​​ or ​​ps​​, but to observe you from a different and unique perspective system. It can be critical when running containers because containers use control groups.

2. Linux scheduled tasks

​Cron​​ is a classic component of Linux. You use Cron when you want to schedule something to happen on a regular basis. It's reliable and integrates into your system fairly well.

The problem is, Cron does not understand that some computers will be shut down. If you have a cron job scheduled for midnight, but you shut down your computer at 23:59 every day, your cron job will never run. Cron doesn't have any tools to detect jobs missed overnight.

As an answer to this question, there is a good one ​​Anacron​​, but it is not as well integrated as Cron. To get Anacron running, you need to do a lot of setup.

The second option is systemd timers. Like Cron, it's built-in and ready to use. You need to write a unit file, which is definitely more work than a single-line Crontab entry, but it's also pretty simple. For example, here's a unit file that runs a hypothetical backup script 30 minutes after boot, but only once a day. This ensures that my computer is backed up and prevents it from trying to back up more than once a day.

[Unit]
Description=Backup
Requires=myBackup.service
[Timer]
OnBootSec=30min
OnUnitActiveSec=1d
[Install]
WantedBy=timers.target
Copy after login

Of course, you can also intervene and prompt to run a task. Thanks to the ​​OnUnitActiveSec​​ directive, systemd will not try to run jobs that you manually activated.

3、运行 Linux 容器

容器使启动一个复杂的服务变得非常容易。你可以在短短几分钟内运行一个 ​​Mattermost​​ 或 Discourse 服务器。在某些情况下,困难的部分是在你运行容器后管理和监控它们。Podman 使得管理它们变得容易,但是用什么来管理 Podman 呢?嗯,​​你可以使用 systemd​​。

Podman 有一个内置的命令来生成单元文件,这样你的容器就可以被 systemd 管理和监控:

$ podman generate systemd --new --files --name example_pod
Copy after login

然后你所要做的就是启动服务:

$ systemctl --user start pod-example_pod.service
Copy after login

和其他服务一样,systemd 确保你的容器荚在任何情况下都能运行。它记录问题,你可以用 ​​journalctl​​ 和其他重要的日志来查看,你也可以用 ​​systemd-cgtop​​ 在控制组中监控它的活动。

它不是 ​​Kubernetes 平台​​,但对于一两个容器来说,你只需要在可靠和可预测的基础上提供服务,Podman 和 systemd 是一对很棒的组合。

下载 systemd 电子书

systemd 的内容还有很多,你可以从作者 ​​David Both​​ 的新书《​​systemd 实用指南​​》中了解基础知识,以及很多实用的技巧。

The above is the detailed content of Three surprising things you can do with systemd. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template