Systemctl 守护进程
Unit 介绍
Systemd 可以管理所有系统资源。不同的资源统称为 Unit(单位)
。
systemctl list-units
命令可以查看当前系统的所有 Unit
systemctl list-units
systemctl list-units --all
systemctl list-units --all --state=inactive
systemctl list-units --failed
systemctl list-units --type=service
|
systemctl status
命令用于查看系统状态和单个 Unit 的状态
systemctl status
sysystemctl status bluetooth.service
systemctl -H root@rhel7.example.com status httpd.service 除了 status 命令,systemctl 还提供了三个查询状态的简单方法,主要供脚本内部的判断语句使用。
systemctl is-active application.service
systemctl is-failed application.service
systemctl is-enabled application.service
|
最常用的是下面这些命令,用于启动和停止 Unit(主要是 service)
sudo systemctl start apache.service
sudo systemctl stop apache.service
sudo systemctl restart apache.service
sudo systemctl kill apache.service
sudo systemctl reload apache.service
sudo systemctl daemon-reload
systemctl show httpd.service
systemctl show -p CPUShares httpd.service
sudo systemctl set-property httpd.service CPUShares=500
|
Unit 的配置文件
Systemd 默认从目录 /etc/systemd/system/
读取配置文件。但是,里面存放的大部分文件都是符号链接,指向目录 /usr/lib/systemd/system/
,真正的配置文件存放在那个目录
systemctl daemon-reload
重新加载配置文件systemctl enable
激活开机启动systemctl disable
撤销开机启动
Unit 配置文件示例
[Unit] Description=example After=network.target
[Install] WantedBy=multi-user.target
[Service] Type=simple User=root Group=root Restart=always
ProtectSystem=full
PrivateDevices=true
WorkingDirectory=/opt ExecStart=/opt/example --http-port=:8080 --env=pro --apollo-appid=example --apollo-url=http://apollo.example.com:80 --apollo-cluster=example KillMode=process KillSignal=SIGTERM
SendSIGKILL=no
RestartSec=1s UMask=007 -------------------------
|
守护进程日志管理
Unit 日志常用操作
sudo journalctl
sudo journalctl -k
sudo journalctl -b sudo journalctl -b -0
sudo journalctl -b -1
sudo journalctl --since="2012-10-30 18:17:16" sudo journalctl --since "20 min ago" sudo journalctl --since yesterday sudo journalctl --since "2015-01-10" --until "2015-01-11 03:00" sudo journalctl --since 09:00 --until "1 hour ago"
sudo journalctl -n
sudo journalctl -n 20
sudo journalctl -f
sudo journalctl /usr/lib/systemd/systemd
sudo journalctl _PID=1
sudo journalctl /usr/bin/bash
sudo journalctl _UID=33 --since today
sudo journalctl -u nginx.service sudo journalctl -u nginx.service --since today
sudo journalctl -u nginx.service -f
journalctl -u nginx.service -u php-fpm.service --since today
sudo journalctl -p err -b
sudo journalctl -b -u nginx.service -o json
sudo journalctl -b -u nginx.serviceqq -o json-pretty
sudo journalctl --disk-usage
sudo journalctl --vacuum-size=1G
sudo journalctl --vacuum-time=1years
|
日志切割转存
sudo du -ah|grep messages sudo rm messages-202101* sudo vim /etc/logrotate.d/messagelog // 新建messagelog配置
/var/log/messages { size 2000000 // 2G rotate 4 // 保存4份 copytruncate compress }
sudo vim /etc/logrotate.d/syslog // 注释引用/var/log/messages
sudo crontab -e // 新增crontab */30 * * * * sudo /usr/sbin/logrotate /etc/logrotate.conf
sudo crontab -l // 参看crontab
|