search
HomeSystem TutorialLINUXsystemd: A super toolbox on Linux

systemd: A super toolbox on Linux

Feb 09, 2024 pm 05:42 PM
linuxlinux tutoriallinux systemlinux commandshell scriptembeddedlinuxGetting started with linuxlinux 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

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:良许Linux教程网. If there is any infringement, please contact admin@php.cn delete
Does the internet run on Linux?Does the internet run on Linux?Apr 14, 2025 am 12:03 AM

The Internet does not rely on a single operating system, but Linux plays an important role in it. Linux is widely used in servers and network devices and is popular for its stability, security and scalability.

What are Linux operations?What are Linux operations?Apr 13, 2025 am 12:20 AM

The core of the Linux operating system is its command line interface, which can perform various operations through the command line. 1. File and directory operations use ls, cd, mkdir, rm and other commands to manage files and directories. 2. User and permission management ensures system security and resource allocation through useradd, passwd, chmod and other commands. 3. Process management uses ps, kill and other commands to monitor and control system processes. 4. Network operations include ping, ifconfig, ssh and other commands to configure and manage network connections. 5. System monitoring and maintenance use commands such as top, df, du to understand the system's operating status and resource usage.

Boost Productivity with Custom Command Shortcuts Using Linux AliasesBoost Productivity with Custom Command Shortcuts Using Linux AliasesApr 12, 2025 am 11:43 AM

Introduction Linux is a powerful operating system favored by developers, system administrators, and power users due to its flexibility and efficiency. However, frequently using long and complex commands can be tedious and er

What is Linux actually good for?What is Linux actually good for?Apr 12, 2025 am 12:20 AM

Linux is suitable for servers, development environments, and embedded systems. 1. As a server operating system, Linux is stable and efficient, and is often used to deploy high-concurrency applications. 2. As a development environment, Linux provides efficient command line tools and package management systems to improve development efficiency. 3. In embedded systems, Linux is lightweight and customizable, suitable for environments with limited resources.

Essential Tools and Frameworks for Mastering Ethical Hacking on LinuxEssential Tools and Frameworks for Mastering Ethical Hacking on LinuxApr 11, 2025 am 09:11 AM

Introduction: Securing the Digital Frontier with Linux-Based Ethical Hacking In our increasingly interconnected world, cybersecurity is paramount. Ethical hacking and penetration testing are vital for proactively identifying and mitigating vulnerabi

How to learn Linux basics?How to learn Linux basics?Apr 10, 2025 am 09:32 AM

The methods for basic Linux learning from scratch include: 1. Understand the file system and command line interface, 2. Master basic commands such as ls, cd, mkdir, 3. Learn file operations, such as creating and editing files, 4. Explore advanced usage such as pipelines and grep commands, 5. Master debugging skills and performance optimization, 6. Continuously improve skills through practice and exploration.

What is the most use of Linux?What is the most use of Linux?Apr 09, 2025 am 12:02 AM

Linux is widely used in servers, embedded systems and desktop environments. 1) In the server field, Linux has become an ideal choice for hosting websites, databases and applications due to its stability and security. 2) In embedded systems, Linux is popular for its high customization and efficiency. 3) In the desktop environment, Linux provides a variety of desktop environments to meet the needs of different users.

What are the disadvantages of Linux?What are the disadvantages of Linux?Apr 08, 2025 am 12:01 AM

The disadvantages of Linux include user experience, software compatibility, hardware support, and learning curve. 1. The user experience is not as friendly as Windows or macOS, and it relies on the command line interface. 2. The software compatibility is not as good as other systems and lacks native versions of many commercial software. 3. Hardware support is not as comprehensive as Windows, and drivers may be compiled manually. 4. The learning curve is steep, and mastering command line operations requires time and patience.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools