更新 clean.sh

This commit is contained in:
星光-k
2025-01-13 18:04:52 +00:00
committed by GitHub
parent b3feb79540
commit e4802e0ee4

View File

@@ -1,27 +1,27 @@
#!/bin/bash
f_name="clean.py"
f_name=clean.py
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
logdir="${dir}/log"
logdir=${dir%/*}
screen_name="clean_screen"
service_name="raspi_e-Paper.service"
hold_time_seconds=10 # 设置维持时间例如300秒5分钟
hold_time_seconds= # 设置维持时间例如300秒5分钟
# 创建一个新的screen会话并运行Python脚本
screen -dmS $screen_name /usr/bin/python3 -u $dir/$f_name > $logdir/log/clean-info.log 2>&1
# 等待维持时间
echo "正在运行脚本,将在 ${hold_time_seconds} 秒后停止服务..."
sleep $hold_time_seconds
# 停止raspi_e-Paper服务
echo "正在停止$service_name服务..."
sudo systemctl stop "$service_name"
sudo systemctl stop $service_name
# 确保日志目录存在
mkdir -p "${logdir}"
# 查找并结束所有与指定screen_name相关的screen会话
pids=$(screen -ls | grep $screen_name | awk -F '.' '{print $1}' | awk '{print $1}')
for pid in $pids; do
screen -S $pid -X quit
done
# 获取并终止所有与脚本相关的进程
pids=$(pgrep -f "$dir/$f_name")
if [ -n "$pids" ]; then
kill -9 $pids
fi
# 使用nohup在后台运行Python脚本并将输出重定向到日志文件
nohup /usr/bin/python3 -u "$dir/$f_name" > "${logdir}/info-clean.log" 2>&1 &
# 等待维持时间
echo "正在运行脚本,将在 ${hold_time_seconds} 秒后停止..."
sleep $hold_time_seconds
echo "脚本运行完成服务已停止screen会话已结束"