Linux 实用操作之系统服务管理

7 minute

systemctl

  1. 立即启动一个服务:systemctl start my.service
  2. 立即停止一个服务:systemctl stop my.service
  3. 重启一个服务:systemctl restart my.service
  4. 重新加载一个服务的配置文件:systemctl reload my.service
  5. 重载所有修改过的配置文件:systemctl daemon-reload
  6. 开启自启动服务:systemctl enable my.service
  7. 取消开启自启动:systemctl disable my.service
  8. 查看是否已经自启动:systemctl is-enabled my.service
  9. 查看服务运行状态:systemctl status my.service
  10. 查看所有服务:systemctl --type service

service & chkconfig

  1. 启动服务:service my.service start
  2. 终止服务:service my.service stop
  3. 重启服务:service my.service restart
  4. 查看服务运行状态:service my.service status
  5. 开启或取消开机自启动:chkconfig my.service on/off
  6. 查看开机自启动列表:chkconfig --list

Unit 配置文件解释

 1- Unit
 2   - Description,服务的描述
 3   - Requires,定义此unit需在哪个daemon启动后才能够启动
 4- Service
 5   - Type,定义启动时的进程行为。它有以下几种值。
 6   - Type=simple,默认值,执行ExecStart指定的命令,启动主进程
 7   - Type=forking,以 fork 方式从父进程创建子进程,创建后父进程会立即退出
 8   - Type=oneshot,一次性进程,Systemd 会等当前服务退出,再继续往下执行
 9   - Type=dbus,当前服务通过D-Bus启动
10   - Type=notify,当前服务启动完毕,会通知Systemd,再继续往下执行
11   - Type=idle,若有其他任务执行完毕,当前服务才会运行
12   - ExecStart,启动当前服务的命令
13   - ExecStartPre,启动当前服务之前执行的命令
14   - ExecStartPost,启动当前服务之后执行的命令
15   - ExecReload,重启当前服务时执行的命令
16   - ExecStop,停止当前服务时执行的命令
17   - ExecStopPost,停止当其服务之后执行的命令
18   - RestartSec,自动重启当前服务间隔的秒数
19   - Restart,定义何种情况 Systemd 会自动重启当前服务,可能的值包括always(总是重启)、on-successon-failureon-abnormalon-aborton-watchdog
20   - TimeoutSec,定义 Systemd 停止当前服务之前等待的秒数
21   - Environment,指定环境变量
22- Install
23   - WantedBy,值是一个或多个Target,当前Unit激活(enable)时,符号链接会放入/etc/systemd/system目录下面以Target名+.wants后缀构成的子目录中
24   - RequiredBy,它的值是一个或多个Target,当前Unit激活(enable)时,符号链接会放入/etc/systemd/system目录下面以Target名+.required后缀构成的子目录中
25   - Alias,当前Unit可用于启动的别名
26   - Also,当前Unit激活(enable)时,会被同时激活的其他Unit

自定义服务启动

服务的管理通过 systemd 进行,systemd 大部分配置文件位于 /usr/lib/systemd/system/ 内,一般不在这进行修改。

修改的位置位于 /etc/systemd/system 内,在这里可以加入自己的服务。

新建 service 文件

 1vim /etc/systemd/system/test.service
 2
 3# test.service
 4[Unit]
 5Description=service test
 6
 7[Service]
 8Type=simple
 9ExecStart=/bin/bash -c " ~/test-service.sh"
10
11[Install]
12WantedBy=multi-user.target

开启服务并观察

 1[root@VM-0-11-centos ~]# systemctl daemon-reload
 2[root@VM-0-11-centos ~]# systemctl start test.service
 3[root@VM-0-11-centos ~]# systemctl status test.service
 4● test.service - service test
 5   Loaded: loaded (/etc/systemd/system/test.service; disabled; vendor preset: disabled)
 6   Active: active (running) since Wed 2022-04-13 00:04:24 CST; 4s ago
 7 Main PID: 15428 (bash)
 8   CGroup: /system.slice/test.service
 9           ├─15428 /bin/bash -c  ~/test-service.sh | at now;
10           ├─15429 /bin/bash -c  ~/test-service.sh | at now;
11           ├─15430 at now
12           └─15432 sleep 30s
13
14Apr 13 00:04:24 VM-0-11-centos systemd[1]: Started service test.

查看 Unit 启动日志

Systemd 统一管理了所有 Unit 的启动日志,因此只需要使用 journalctl 命令就可以查看到服务的日志。

  1. 显示尾部指定行数的日志:journalctl -n 20
  2. 查看指定服务的日志:journalctl /usr/lib/systemd/systemd
  3. 查看指定进程的日志:journalctl _PID=1
  4. 查看某个 Unit 的日志:journalctl -u nginx.service

通过 PM2 管理应用服务

 1# 启动名为 xxx 的 python 应用
 2pm2 start api.py --name xxx --interpreter ~/Codes/scripts/.venv/bin/python3
 3# 重启 xxx 应用
 4pm2 restart xxx
 5# 列出 pm2 应用
 6pm2 l
 7# 监视 pm2 所有应用
 8pm2 dash
 9# 查看应用日志
10pm2 logs xxx
11# 保存 pm2 应用列表快照
12pm2 save
13# 从快照恢复 pm2 应用列表
14pm2 resurrect