mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
Merge pull request #28946 from lkxed/20230323-2-3-surprising-things-Linux-sysadmins-can-do-with-systemd
[手动选题][tech]: 20230323.2 ⭐️ 3 surprising things Linux sysadmins can do with systemd.md
This commit is contained in:
commit
583960f21a
@ -0,0 +1,102 @@
|
||||
[#]: subject: "3 surprising things Linux sysadmins can do with systemd"
|
||||
[#]: via: "https://opensource.com/article/23/3/3-things-you-didnt-know-systemd-could-do"
|
||||
[#]: author: "Alan Smithee https://opensource.com/users/alansmithee"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: " "
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
3 surprising things Linux sysadmins can do with systemd
|
||||
======
|
||||
|
||||
When it first started out, there was a lot of press about systemd and its ability to speed up boot time. That feature had a mostly-universal appeal (it's less important to those who don't reboot), so in many ways, that's the reputation it still has today. And while it's true that systemd is the thing that launches services in parallel during startup, there's a lot more to it than that. Here are three things you may not have realized systemd could do but should be taking advantage. Get more tips from our new downloadable eBook, **[A pragmatic guide to systemd][1]**.
|
||||
|
||||
### 1. Simplify Linux ps
|
||||
|
||||
If you've ever used the `ps` or even just the `top` command, then you know that your computer is running hundreds of processes at any given moment. Sometimes, that's exactly the kind of information you need in order to understand what your computer, or its users, are up to. Other times, all you really need is a general overview.
|
||||
|
||||
The `systemd-cgtop` command provides a simple view of your computer's load based on the cgroups (control groups) tasks have been arranged into. [Control groups][2] are important to modern Linux, and are essentially the support structures underneath containers and Kubernetes (which in turn are why the cloud scales the way it does), but also they're useful constructs on your home PC. For instance, from the output of `systemd-cgtop`, you can see the load of your user processes as opposed to 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 just your userspace processes, or just your userspace processes and kernel threads.
|
||||
|
||||
This isn't a replacement for `top` or `ps` by any means, but it's an additional view into your system from a different and unique angle. And it can be vital when running containers, because containers use cgroups.
|
||||
|
||||
### 2. Linux cron
|
||||
|
||||
[Cron][3] is a classic component of Linux. When you want to schedule something to happen on a regular basis, you use cron. It's reliable and pretty well integrated into your system.
|
||||
|
||||
The problem is, cron doesn't understand that some computers get shut down. If you have a cronjob scheduled for midnight, but you turn your computer off at 23:59 every day, then your cronjob never runs. There's no facility for cron to detect that there was a missed job overnight.
|
||||
|
||||
As an answer to that problem, there's the excellent [anacron][4], but that's not quite as integrated as cron. There's a lot of setup you have to do to get anacron running.
|
||||
|
||||
A second alternative is systemd timers. Like cron, it's already built in and ready to go. You have to write a unit file, which is definitely more lines than a one-line crontab entry, but it's also pretty simple. For instance, here's a unit file to run an imaginary backup script 30 minutes after startup, but only once a day. This ensures that my computer gets backed up, and prevents it from trying to backup more than once daily.
|
||||
|
||||
```
|
||||
[Unit]
|
||||
Description=Backup
|
||||
Requires=myBackup.service
|
||||
|
||||
[Timer]
|
||||
OnBootSec=30min
|
||||
OnUnitActiveSec=1d
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
```
|
||||
|
||||
You can, of course, intervene and prompt a job to run with . Thanks to the `OnUnitActiveSec` directive, systemd doesn't attempt to run a job you've manually activated.
|
||||
|
||||
### 3. Run Linux containers
|
||||
|
||||
Containers make starting up a complex service really easy. You can run a [Mattermost][5] or Discourse server in mere minutes. The hard part, in some cases, is managing and monitoring the containers once you have them running. Podman makes it easy to manage them, but what do use to manage Podman? Well, [you can use systemd][6].
|
||||
|
||||
Podman has a built-in command to generate unit files so your containers can be managed and monitored by systemd:
|
||||
|
||||
```
|
||||
$ podman generate systemd --new --files --name example_pod
|
||||
```
|
||||
|
||||
All you have to do then is start the service:
|
||||
|
||||
```
|
||||
$ systemctl --user start pod-example_pod.service
|
||||
```
|
||||
|
||||
As with any other service on your computer, systemd ensures that your pod runs no matter what. It logs problems, which you can view with `journalctl` along with your other essential logs, and you can monitor its activity within cgroups using `systemd-cgtop`.
|
||||
|
||||
It's no [Kubernetes platform][7], but for one or two containers that you just want to have available on a reliable and predictable basis, Podman and systemd are an amazing pair.
|
||||
|
||||
### Download the systemd eBook
|
||||
|
||||
There's a lot more to systemd, and you can learn the basics, along with lots of useful and pragmatic tips, from author [David Both][8] in his new complimentary **[pragmatic guide to systemd][1]**.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/23/3/3-things-you-didnt-know-systemd-could-do
|
||||
|
||||
作者:[Alan Smithee][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/alansmithee
|
||||
[b]: https://github.com/lkxed/
|
||||
[1]: https://opensource.com/downloads/pragmatic-guide-systemd-linux
|
||||
[2]: https://www.redhat.com/sysadmin/cgroups-part-four?intcmp=7013a000002qLH8AAM
|
||||
[3]: https://opensource.com/article/17/11/how-use-cron-linux
|
||||
[4]: https://opensource.com/article/21/2/linux-automation
|
||||
[5]: https://opensource.com/education/16/3/mattermost-open-source-chat
|
||||
[6]: https://www.redhat.com/sysadmin/podman-run-pods-systemd-services?intcmp=7013a000002qLH8AAM
|
||||
[7]: https://www.redhat.com/en/technologies/cloud-computing/openshift/aws?intcmp=7013a000002qLH8AAM
|
||||
[8]: https://opensource.com/users/dboth
|
Loading…
Reference in New Issue
Block a user