Home  >  Article  >  System Tutorial  >  systemd: A super toolbox on Linux

systemd: A super toolbox on Linux

王林
王林forward
2024-02-09 17:42:20880browse

systemd:Linux 上的超级工具箱

systemd is a modern initialization system on Linux. It is responsible for managing system startup, service running, user login, etc. The goal of systemd is to provide a fast, flexible, and scalable system management framework. However, systemd is not just an initialization system, it also provides many other functions and tools to make it easier for you to manage and monitor your Linux system. In this article, we’ll take a look at three amazing things you can do with systemd:

  • Simplifying Linux ps: Use the systemd-cgtop command to view system load based on control groups
  • Linux scheduled tasks: Use systemd timers to schedule regularly executed tasks
  • Running Linux Containers: Manage and Monitor Containers with Podman and systemd

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 know that your computer has hundreds of processes running at all times. 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 The 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 supporting 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 on user processes, not system processes:

Control Group               Proc+   %CPU   Memory  Input/s Output/s
/                             183    5.0     1.6G       0B     3.0M
user.slice                      4    2.8     1.1G       0B   174.7K
user.slice/user-1000.slice      4    2.8   968.2M       0B   174.7K
system.slice                   65    2.2     1.5G       0B     2.8M

You can also view only your user space processes, or view user space processes and kernel threads.

This is by no means a replacement for top or ps, but rather a different, unique perspective on your 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 Anacron which is good, 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

当然,你也可以干预并提示运行一个任务。多亏了 OnUnitActiveSec 指令,systemd 不会试图运行你手动激活的作业。

3、运行 Linux 容器

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

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

$ podman generate systemd --new --files --name example_pod

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

$ systemctl --user start pod-example_pod.service

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

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

本文介绍了三件可以用 systemd 做的令人惊讶的事情,它们可以让你更轻松地管理和监控你的 Linux 系统。systemd 的内容还有很多,你可以从作者 David Both 的新书《systemd 实用指南》中了解更多。希望本文对你有所帮助!

The above is the detailed content of systemd: A super toolbox on Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete