Table of Contents
1. Simplify Linux ps" >1. Simplify Linux ps
2. Linux scheduled tasks" >2. Linux scheduled tasks
3、运行 Linux 容器" >3、运行 Linux 容器
Home System Tutorial LINUX systemd: A super toolbox on Linux

systemd: A super toolbox on Linux

Feb 09, 2024 pm 05:42 PM
linux linux tutorial linux system linux command shell script embeddedlinux Getting started with linux linux learning

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
Copy after login

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
Copy after login

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

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 做的令人惊讶的事情,它们可以让你更轻松地管理和监控你的 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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

vscode terminal command cannot be used vscode terminal command cannot be used Apr 15, 2025 pm 10:03 PM

Causes and solutions for the VS Code terminal commands not available: The necessary tools are not installed (Windows: WSL; macOS: Xcode command line tools) Path configuration is wrong (add executable files to PATH environment variables) Permission issues (run VS Code as administrator) Firewall or proxy restrictions (check settings, unrestrictions) Terminal settings are incorrect (enable use of external terminals) VS Code installation is corrupt (reinstall or update) Terminal configuration is incompatible (try different terminal types or commands) Specific environment variables are missing (set necessary environment variables)

See all articles