

新闻资讯
行业动态日志轮转配置不当、systemd重启策略缺失、slab内存泄漏、时间同步失效是Linux服务器长期运行后崩溃的四大隐形杀手,需分别通过logrotate定制、RestartSec/StartLimitInterval设置、slab监控和timedatectl校准来根治。
长期运行的 Linux 服务器最常见崩溃原因之一是 /var/log 目录被撑爆,尤其当应用未做日志切割、rsyslog 或 logrotate 配置缺失时。默认的 logrotate 策略往往只覆盖 /var/log/messages 等系统日志,而忽略服务自建日志(如 /opt/myapp/logs/app.log)。
logrotate -d /etc/logrotate.conf(-d 表
示 debug 模式,不实际执行)/etc/logrotate.d/myapp 中写入/opt/myapp/logs/*.log {
daily
missingok
rotate 30
compress
delaycompress
notifempty
create 0644 myapp myapp
sharedscripts
postrotate
systemctl kill --signal=SIGHUP myapp.service > /dev/null 2>&1 || true
endscript
}logrotate -f /etc/logrotate.d/myapp 强制触发一次,确认日志重命名、压缩、服务重载均无报错copytruncate 替代服务重启——某些程序(如 Java 进程用 logback)在文件被 truncate 后可能继续写入旧 inode,导致日志丢失很多运维人员只加了 Restart=always,却没配 RestartSec 和 StartLimitInterval,结果服务秒级反复崩溃时,systemd 会直接放弃拉起,并标记为 failed,且不再尝试——这在无人值守的边缘设备上尤为致命。
[Service] Restart=on-failure RestartSec=10 StartLimitInterval=60 StartLimitBurst=3
StartLimitInterval=60 + StartLimitBurst=3 表示:60 秒内最多允许失败 3 次,超限后 systemd 将拒绝后续启动请求,直到间隔过去RestartSec=10 强制退避,避免 CPU 打满或下游雪崩;对数据库连接失败类问题,还应配合 ExecStartPre=/bin/sleep 5 做前置缓冲systemctl show myapp.service | grep -E "(Restart|StartLimit)",确保值已生效长期运行中,有些泄漏不体现在 top 的 %MEM 或 RES 列,而是藏在内核 slab 分配器里(如 dentry、inode_cache、ext4_inode_cache)。这类泄漏不会被 ps 或 htop 显示,但最终触发 Out of memory: Kill process,且 oom_killer 往往误杀无辜进程。
cat /proc/meminfo | grep -i slab,重点关注 SReclaimable 和 SUnreclaim
sudo cat /proc/slabinfo | awk '$3 > 100000 {print $1, $3}' | sort -k2 -n(筛选活跃对象数超 10 万的 slab 缓存)journal 且未调优 commit=60
echo 2 > /proc/sys/vm/drop_caches(仅释放 pagecache + dentries + inodes),但不能替代根因修复看似无关的时间问题,实则是长期稳定性隐形杀手。当 ntpd 或 systemd-timesyncd 失效超过数小时,系统时钟漂移(drift)可能达分钟级,直接导致:cron 任务跳过或重复执行、systemd timer 触发异常、HTTPS 请求因证书 NotBefore/NotAfter 时间校验失败而中断。
timedatectl set-ntp true,并确认 systemd-timesyncd 处于 active 状态ntpd 与 systemd-timesyncd 共存——二者冲突会导致时钟跳跃(jump)而非平滑调整(slew)timedatectl status 中 System clock synchronized: yes 和 NTP service: active 必须同时为 truechrony 并配置 makestep 1.0 -1,允许首次启动时快速校正大偏差