centos7使用systemd管理程序
systemd的配置文件主要放在/usr/lib/systemd/system
目录,也可能在/etc/systemd/system
目录。
学习下sshd的系统配置文件写法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| $cat sshd.service
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service
Wants=sshd-keygen.service
[Service]
Type=forking
PIDFile=/var/run/sshd.pid
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
|
简单解释下:
配置一个自己的系统服务
日常启动程序方式:
1
| ./etc/test/sipholeWatchDog/sipholeWatchDog
|
现在添加配置通过systemd
来管理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| vi /usr/lib/systemd/system/sipholeWatchDog.service
添加如下配置:
[Unit]
Description=sipholeWatchDog
After= syslog.target network.target jtsec-firstrun.service
[Service]
Type=forking
ExecStart=/etc/test/sipholeWatchDog/sipholeWatchDog
UMask=0007
RestartSec=10
Restart=always
[Install]
WantedBy=multi-user.target
|
加载单元
1
| systemctl daemon-reload
|
启动服务并查看服务状态
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| systemctl start sipholeWatchDog
systemctl status sipholeWatchDog
● sipholeWatchDog.service - sipholeWatchDog
Loaded: loaded (/usr/lib/systemd/system/sipholeWatchDog.service; enabled; vendor preset: disabled)
Active: active (running) since 二 2023-01-03 11:10:57 CST; 14min ago
Main PID: 22088 (sipholeWatchDog)
CGroup: /system.slice/sipholeWatchDog.service
├─22088 /etc/test/sipholeWatchDog/sipholeWatchDog
├─22269 /etc/siphole/siphole_back -c /etc/siphole/siphole_back_5.conf
└─22281 journalctl _PID=22269 -f
1月 03 11:24:59 video_back siphole_back[22269]: msp-P28181: 0:videoSrvId:00:RUN: MESSAGE: CmdType: RecordInfo Devices: 3301...001389
1月 03 11:25:03 video_back siphole_back[22269]: msp-P28181: 0:videoSrvId:00:Find Route ...
1月 03 11:25:03 video_back siphole_back[22269]: msp-P28181: 0:videoSrvId:00:RUN: MESSAGE: CmdType: RecordInfo Devices: 3301...001389
1月 03 11:25:11 video_back siphole_back[22269]: msp-P28181: 0:videoSrvId:00:Find Route ...
1月 03 11:25:11 video_back siphole_back[22269]: msp-P28181: 0:videoSrvId:00:RUN: MESSAGE: CmdType: RecordInfo Devices: 3301...001391
1月 03 11:25:12 video_back siphole_back[22269]: msp-P28181: 0:videoSrvId:00:Find Route ...
1月 03 11:25:12 video_back siphole_back[22269]: msp-P28181: 0:videoSrvId:00:RUN: MESSAGE: CmdType: RecordInfo Devices: 3301...001391
1月 03 11:25:13 video_back siphole_back[22269]: RUN:plugin/p28181.c 636:videoSrvId:00:Total devices flow: 20 bytes. 0 medias.
1月 03 11:25:14 video_back siphole_back[22269]: msp-P28181: 0:videoSrvId:00:Find Route ...
1月 03 11:25:14 video_back siphole_back[22269]: msp-P28181: 0:videoSrvId:00:RUN: MESSAGE: CmdType: RecordInfo Devices: 3301...001391
Hint: Some lines were ellipsized, use -l to show in full.
|
可以加入开机启动
1
| systemctl enable sipholeWatchDog
|
systemctl
命令整理
1
| systemctl 或 systemctl list-units
|
1
| systemctl --type=service
|
1
| systemctl is-active sshd
|
1
2
3
| systemctl start sshd.service
systemctl stop sshd.service
systemctl restart sshd.service
|
1
| systemctl reload sshd.service
|
1
| systemctl list-units --type=service
|
1
| systemctl list-units --type=service --all
|
1
| systemctl list-unit-files --type=service
|
1
| systemctl list-dependencies sshd
|
1
| systemctl is-enabled sshd
|
1
| systemctl disable network
|
1
| systemctl enable network
|
systemd日志管理
systemd 提供了自己日志系统(logging system),称为 journal. 使用 systemd 日志,无需额外安装日志服务(syslog)。读取日志的命令:
1
| journalctl --disk-usage
|
1
| journalctl --vacuum-size=500M
|
Linux log日志占用过大清理方法
Linux
使用df -h
检查磁盘文件,可以看到/run
目录下有日志目录/run/log/journal
,占用了数G空间。
或者直接在相应目录下执行du --max-depth=1 -h
Linux log 日志清理
检查当前journal磁盘占用量
1
| journalctl --disk-usage
|
清理方法可以采用按照日期清理,或者按照允许保留的容量清理,只保存7天的日志,最大500M。
1
2
| journalctl --vacuum-time=7d
journalctl --vacuum-size=500M
|
如果要手工删除日志文件,则在删除前需要先轮转一次journal日志。
1
| systemctl kill --kill-who=main --signal=SIGUSR2 systemd-journald.service
|
要启用日志限制持久化配置,可以修改 /etc/systemd/journald.conf
1
2
| SystemMaxUse=16M
ForwardToSyslog=no
|
然后重启
1
| systemctl restart systemd-journald.service
|
检查journal是否运行正常以及日志文件是否完整无损坏
警告
本文最后更新于 January 3, 2023,文中内容可能已过时,请谨慎使用。