From e4802e0ee429682a055617ebdb0653fb17576dee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=85=89-k?= <36470587+kxgx@users.noreply.github.com> Date: Mon, 13 Jan 2025 18:04:52 +0000 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20clean.sh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/clean.sh | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/bin/clean.sh b/bin/clean.sh index d261bae..cdf3b3a 100644 --- a/bin/clean.sh +++ b/bin/clean.sh @@ -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会话已结束" \ No newline at end of file