Blog

sub:cgroup

Terminology #

  1. cgroups a cgroup is a collection of processes. The processes within each cgroup are bound to limits and parameters that are defined within the cgroup filesystem.
  2. services 用于组织和管理与系统服务相关的进程的 cgroup。
  3. scopes 用于组织和管理用户会话或任务组的 cgroup。
  4. slices a group of hierarchically organized units. A slice manages processes that are running in either scopes or services.

Controlling CPU usage for a user(systemd) Controlling CPU usage for a service(systemd) Controlling memory usage for user(systemd) Controlling blkio usage(systemd) Setting resource limits on rootless containers Setting cpuset(systemd) Converting to cgroup version 2 给firefox进程及子进程添加mark

...

Shutdown and Reboot(systemd)

Content #

Doing the Ctrl+Alt+Del key sequence 7 times within 2 seconds will force the machine to do a reboot. (SIGINT section of the systemd man page)

Using Ctrl+Alt+Del to reboot a machine doesn’t require root privileges.

sudo systemctl poweroff

shutdown target is defined in the systemd executable file:

strings systemd | gerp 'shutdown.target'

From #

Changing the default locale

Content #

localectl
sudo localectl set-locale en-CA.utf8
sudo localectl set-keymap ca
sudo localectl set-x11-keymap ca

Any time that you use localectl to change a setting, localectl will start the systmed-localed service by sending it a dbus message.

Ubuntu系统中需要先修改/etc/default/locale文件,然后执行下面的命令:

sudo locale-gen

From #

systemd timers

Content #

systemctl list-unit-files -t timer
systemctl list-timers

Monotonic timers #

Run after some sofrt of event that serves as a starting point. Check all of the monotonic starting points by looking at the systemd.timer man pages.

Passive targets(systemd)

Content #

network.target中有如下配置:

RefuseManualStart=yes

这意味着network会被自动启动,无法手动启动。 network.target配置中看不到任何服务,对应的wants目录也不存在。看起来像是根本没有任何作用。真正的情况是这些服务直接在systemd的二进制文件中实现了。通过下面的命令可以看到硬编码的target:

strings /lib/systemd/systemd | grep '\.target'

From #

target(systemd)

Content #

A target is a unit that groups together other systemd units for a particular purpose. The units that a target can group together include services, paths, mount points, sockets, and even other targets.

systemctl list-units -t target
systemctl list-units -t target --state=inactive

Passive targets(systemd)

From #

man systemd.special

Create a new container service with podman from user account

Content #

podman run -d -p 9080:80 --name wordpress-noroot wordpress
podman container stop wordpress-noroot
mkdir -p ~/.config/systemd/user
podman generate systemd wordpress-noroot > ~/.config/systemd/user/wordpress-noroot.service
systemctl --user daemon-reload
systemctl --user status wordpress-noroot
systemctl --user enable --now wordpress-noroot

From #

Masking a service

Content #

Now, let’s say that you have a service that you never want to start, either manually or automatically. You can accomplish this by masking the service, like this:

[donnie@localhost ~]$ sudo systemctl mask httpd
Created symlink /etc/systemd/system/httpd.service → /dev/null.

This time, instead of creating a symbolic link that points back to the service file, we’ve created one that points to the /dev/null device. Let’s try to start our masked Apache service to see what happens:

...

unclean kill signal

Content #

由于apache2服务会启动多个进程,直接用kill命令会导致部分进程成为僵尸进程,可用下面的命令终止apache2进程:

sudo systemctl kill apache2

systemd向进程发送的信号为默认的SIGTERM(15),这被认为是clean signal,即 apache2服务能够在接收到该信号后做清理工作,再结束进程。验证:

systemctl is-active apache2
inactive

SIGKILL信号被认为是unclean signal,进程会被直接结束掉,没有做结束的清理工作:

sudo systemctl kill -s SIGKILL apache2
systemctl is-active apache2
active

之所以会看到active,是因为apache2的配置文件中有:

Restart=on-abort

在收到unclean signal时,会重启apache2进程。

From #