systemctl 启动服务缓慢

服务启动缓慢

使用systemd配置服务启动慢

strace -s 1024 systemctl start AAA

有 -1 : EAGAIN (Resource temporarily unavailable) 报错及卡顿, 查看了服务配置依赖项

/etc/systemd/system/multi-user.target.wants 目录下找到服务配置文件, 查看对应配置文件 Unit配置项里的Requires值, 查找对应依赖服务发现有一个服务异常, 这个服务启动脚本中启动network服务, 由于配置网卡错误导致无法启动。

 

systemd

systemd 是 Linux 操作系统的系统和服务管理器系统。 当作为启动时的第一个进程(作为 PID 1)运行时,它充当
启动和维护用户空间服务的 init 系统。为登录用户启动单独的实例来启动他们的服务。

garlic@garlic:~$ ls -l /sbin/init
lrwxrwxrwx 1 root root 20 Mar 20  2023 /sbin/init -> /lib/systemd/systemd
garlic@garlic:~$ uname -a
Linux garlic 6.2.0-35-generic #35-Ubuntu SMP PREEMPT_DYNAMIC Tue Oct  3 13:14:56 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

 

创建一个服务

之前写的应用软件自己写daemon进程,然后通过数据库完成配置管理,然后通过一个工具实现服务器启动关闭。简单的应用也是够用的。

 

/etc/systemd/system/garlic.service

garlic@garlic:/etc/systemd/system$ cat garlic.service
[Unit]
Description=garlic demo service
After=newwork.target
StartLimitIntervalSec=0

[Service]
Type=simple
Restart=always
ExecStart=python3 -m http.server 8080

[Install]
WantedBy=multi-user.targetq

启动

garlic@garlic:/etc/systemd/system$ sudo systemctl start garlic.service
garlic@garlic:/etc/systemd/system$ sudo systemctl status garlic.service
● garlic.service - garlic demo service
     Loaded: loaded (/etc/systemd/system/garlic.service; disabled; preset: enabled)
     Active: active (running) since Sat 2023-10-21 10:04:27 UTC; 4s ago
   Main PID: 4392 (python3)
      Tasks: 1 (limit: 4496)
     Memory: 9.2M
        CPU: 85ms
     CGroup: /system.slice/garlic.service
             └─4392 python3 -m http.server 8080

Oct 21 10:04:27 garlic systemd[1]: Started garlic.service - garlic demo service

关闭

garlic@garlic:/etc/systemd/system$ sudo systemctl stop garlic.service
garlic@garlic:/etc/systemd/system$ sudo systemctl status garlic.service
○ garlic.service - garlic demo service
     Loaded: loaded (/etc/systemd/system/garlic.service; disabled; preset: enabled)
     Active: inactive (dead)

Oct 21 10:03:05 garlic systemd[1]: garlic.service: Deactivated successfully.
Oct 21 10:03:05 garlic systemd[1]: Stopped garlic.service - garlic demo service.
Oct 21 10:03:10 garlic systemd[1]: Started garlic.service - garlic demo service.
Oct 21 10:03:12 garlic systemd[1]: Stopping garlic.service - garlic demo service...
Oct 21 10:03:12 garlic systemd[1]: garlic.service: Deactivated successfully.
Oct 21 10:03:12 garlic systemd[1]: Stopped garlic.service - garlic demo service.
Oct 21 10:04:27 garlic systemd[1]: Started garlic.service - garlic demo service.
Oct 21 10:05:38 garlic systemd[1]: Stopping garlic.service - garlic demo service...
Oct 21 10:05:38 garlic systemd[1]: garlic.service: Deactivated successfully.
Oct 21 10:05:38 garlic systemd[1]: Stopped garlic.service - garlic demo service.

systemd 还有一些其他的命令hostnamectl, timedatectl

 

参考及引用

https://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html

图片from洪鐘富

 

 

Comments are closed.