<QNAP 453D QTS-5.x> 日志记录:使用 shell 脚本去优化性能 清理内存 使 crontab 做到每6小时执行 永久改变

起因:

机械硬盘的声音又来了,这还是在我停掉了一个有 Whisper 模型的 Container 后发生的。

检查过程:

还是有 swap 内存占用。

之前做的更改,因为 QNAP QTS 升级 (相当于 OS 更新),或是重启 NAS 消失了。

可参考看我上一篇文章里的截图:<QNAP 453D QTS-5.x> 日志记录: 优化性能 内存管理 修改swap优先顺序 swap放在ssd 网络稳定性 进程出错管理_威联通 卡顿 swap-CSDN博客

没想到 QNAP NAS 有这个功能,也是还原是保持系统稳定的设计,毕竟是封闭的 Linux。

但这次变化还是有的, 当前版本没有了: /etc/sysctl.conf 文件

当时没有对 Firmware 留影,这次存下:

用脚本修改 sysctl.conf 与 swap 优先级:

考虑到 /share 目录是保持不变的, 可以写脚本实现。

所有文件放到:[/share/Multimedia/2024-MyProgramFiles/29.QTS_conf_files] 

用脚本检查,更新;将使用 crontab 来运行。 如果 crontab 会被系统回退(恢复),可能要找客服技术问问。

1. sysctl.conf 

[admin@davens templates]# sysctl -p
vm.swappiness = 10
vm.oom_kill_allocating_task = 1
vm.min_free_kbytes = 65536
vm.vfs_cache_pressure = 50
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.core.netdev_max_backlog = 2500
kernel.panic = 20
kernel.panic_on_oops = 1
[admin@davens templates]#

2. swap

这时发现 /share 文件还在,就按之前的文章调整。

就调整一下顺序,让 SSD (/share/CACHEDEV1_DATA/.swap/qnap_swap) 在第一优先。

我第一份工作时,看完了一本叫 Linux 命令 shell 脚本编程的书,之后开始学 Oracle SQLplus,入门这两,太难找工作。然后就去考 MCSE CCNP ,最主要是考试费便宜,0DAY上还有BIBLE题库... 容易在外企找到工作啊。

3. Shell 脚本 selfscript.sh

a. 功能:

  • 检查 /etc/sysctl.conf 文件的存在,并备份。
  • 替换 sysctl.conf 
  • 更改 swap 优先级
  • 更改 crontab 中,清理内存进程的频率,默认是在 1 am 运行,改为每 6 小时运行。

b. 脚本:selfscript.sh

#!/bin/bash
# created on 8Jan2025 by Dave
# ver. 0.6

# Check root execute this script.
if [ "$(id -u)" != "0" ]; then
    echo "This script must be run as root"
    echo "Please use: sudo $0"
    exit 1
fi

# Define variables
LOG_FILE="/share/Multimedia/2024-MyProgramFiles/29.QTS_conf_files/selfscript.log"
SYSCTL_SOURCE="/share/Multimedia/2024-MyProgramFiles/29.QTS_conf_files/sysctl.conf"
SYSCTL_BACKUP="/share/Multimedia/2024-MyProgramFiles/29.QTS_conf_files/sysctl.conf.bk"
SYSCTL_DEST="/etc/sysctl.conf"

# logging timestamp
log_message() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE"
}

# error handling
handle_error() {
    local error_msg="$1"
    log_message "ERROR: $error_msg"
    echo "$(date '+%Y-%m-%d %H:%M:%S') - WARNING: sysctl.conf update failed"
    exit 1
}

# Start script execution
log_message "======================"
log_message "Starting system configuration script"

# check required files exist
if [ ! -f "$SYSCTL_SOURCE" ]; then
    handle_error "Source configuration file $SYSCTL_SOURCE does not exist"
fi

# Part one: Process sysctl.conf
log_message "Starting sysctl.conf processing"

if [ -f "$SYSCTL_DEST" ]; then
    line_count=$(wc -l < "$SYSCTL_DEST")
    log_message "Current sysctl.conf line count: $line_count"
    
    if [ "$line_count" -lt 5 ]; then
        log_message "sysctl.conf has less than 5 lines, update needed"
        
        if [ -f "$SYSCTL_BACKUP" ]; then
            if cmp -s "$SYSCTL_DEST" "$SYSCTL_BACKUP"; then
                log_message "Current file matches backup, proceeding with update"
                cp "$SYSCTL_SOURCE" "$SYSCTL_DEST" || handle_error "Failed to update sysctl.conf"
            else
                log_message "Files differ, creating backup before update"
                cp "$SYSCTL_DEST" "${SYSCTL_DEST}.backup.$(date +%Y%m%d)" || handle_error "Backup creation failed"
                cp "$SYSCTL_SOURCE" "$SYSCTL_DEST" || handle_error "Failed to update sysctl.conf"
            fi
        else
            log_message "Backup file not found, proceeding with direct update"
            cp "$SYSCTL_SOURCE" "$SYSCTL_DEST" || handle_error "Failed to update sysctl.conf"
        fi
    else
        log_message "sysctl.conf line count normal, no update needed"
    fi
else
    log_message "sysctl.conf not found, creating new file"
    cp "$SYSCTL_SOURCE" "$SYSCTL_DEST" || handle_error "Failed to create sysctl.conf"
fi

# Apply sysctl conf
log_message "Applying new sysctl configuration"
if ! sysctl -p > /tmp/sysctl.out 2>&1; then
    # If there's an error, show the output and log it
    error_output=$(cat /tmp/sysctl.out)
    log_message "WARNING: sysctl error: $error_output"
    echo "WARNING: Error applying sysctl settings: $error_output"
else
    # On success, only log the output but don't show on screen
    log_message "sysctl configuration applied successfully:"
    cat /tmp/sysctl.out >> "$LOG_FILE"
fi
#rm -f /tmp/sysctl.out

# Part Two: swap file priority adjustment
log_message "Beginning swap priority adjustment"

# First, Recording current swap status
log_message "Current swap configuration:"
cat /pro
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值