目录

[TOC]


前言

近来莫名自动重启,想要通过日志排查问题,结果发现系统日志重启后就不见了,就想着把日志手动写入外部路径/mnt/local(外部路径需自己挂载),保证重启后还在

1.新建文件/mnt/local/scripts/SystemLog.sh,并给可执行权限

mkdir -p /mnt/local/scripts && touch /mnt/local/scripts/SystemLog.sh && chmod +x /mnt/local/scripts/SystemLog.sh

2.将如下脚本粘贴进/mnt/local/scripts/SystemLog.sh

其中保存日志文件的路径根据自己的需要修改

#!/bin/bash

# 保存日志文件的路径
LOG_FILE="/mnt/local/opwnert/system.log"

# 执行logread命令,并将结果追加到日志文件中
logread >> $LOG_FILE

# 循环执行logread命令,只要有新的日志行就将其追加到日志文件中
while true
do
    logread -f | while read line
    do
        echo $line >> $LOG_FILE
    done
done

3.设置脚本开机自启

3.1 编辑/etc/rc.local

vim /etc/rc.local

3.2 在/etc/rc.local中的exit 0上面的任意位置添加:

bash /mnt/local/scripts/SystemLog.sh &

3.3 :wq保存退出

4.重启即可

默认的系统日志路径为:
LOG_FILE="/mnt/local/opwnert/system.log"

Last modification:April 11, 2023
V50%看看实力