~重写 set_cron.sh 尝试修复因为定时任务导致系统crontab内容丢失的问题

This commit is contained in:
juewuy
2026-02-02 19:23:03 +08:00
parent 3682a99a79
commit 1bd16b5ca6
5 changed files with 32 additions and 28 deletions

View File

@@ -1,27 +1,38 @@
crondir="$(crond -h 2>&1 | grep -oE 'Default:.*' | awk -F ":" '{print $2}')" crondir="$(crond -h 2>&1 | grep -oE 'Default:.*' | awk -F ":" '{print $2}'| tr -d ' ')"
[ ! -w "$crondir" ] && crondir="/etc/storage/cron/crontabs" [ ! -w "$crondir" ] && crondir="/etc/storage/cron/crontabs"
[ ! -w "$crondir" ] && crondir="/var/spool/cron/crontabs" [ ! -w "$crondir" ] && crondir="/var/spool/cron/crontabs"
[ ! -w "$crondir" ] && crondir="/var/spool/cron" [ ! -w "$crondir" ] && crondir="/var/spool/cron"
[ -z "$USER" ] && USER=$(whoami 2>/dev/null)
tmpcron=/tmp/cron_tmp tmpcron=/tmp/cron_tmp
touch "$tmpcron" touch "$tmpcron"
croncmd() { #定时任务工具 cronadd() { #定时任务工具
if [ -w "$crondir" ] && [ -n "$USER" ];then if crontab -h 2>&1 | grep -q '\-l'; then
[ "$1" = "-l" ] && cat "$crondir"/"$USER" 2>/dev/null
[ -f "$1" ] && cat "$1" >"$crondir"/"$USER"
killall -HUP crond 2>/dev/null
elif [ -n "$(crontab -h 2>&1 | grep '\-l')" ]; then
crontab "$1" crontab "$1"
else
cat "$1" >"$crondir"/"$USER" && cru a REFRESH "0 0 1 1 * /bin/true" 2>/dev/null
fi
}
cronload() { #定时任务工具
if [ -f "$crondir/$USER" ];then
cat "$crondir"/"$USER" 2>/dev/null
elif crontab -h 2>&1 | grep -q '\-l'; then
crontab -l
else else
echo "找不到可用的crond或者crontab应用No available crond or crontab application can be found!" echo "找不到可用的crond或者crontab应用No available crond or crontab application can be found!"
fi fi
} }
cronset() { #定时任务设置 cronset() { #定时任务设置
# 参数1代表要移除的关键字,参数2代表要添加的任务语句 # 参数1代表要移除的关键字,参数2代表要添加的任务语句
croncmd -l >"$tmpcron" cronload | grep -v '^$' | grep -vF "$1" >"$tmpcron"
sed -i "/$1/d" "$tmpcron" [ -n "$2" ] && echo "$2" >>"$tmpcron"
echo "$2" >>"$tmpcron" cronadd "$tmpcron"
croncmd "$tmpcron" #华硕/Padavan固件存档在本地,其他则删除
rm -f "$tmpcron" if [ -d /jffs ] || [ -d /etc/storage/ShellCrash ];then
mv -f "$tmpcron" "$CRASHDIR"/task/cron
else
rm -f "$tmpcron"
fi
sleep 1
} }

View File

@@ -108,9 +108,7 @@ task_add(){ #任务添加
} }
task_del(){ #任务删除 task_del(){ #任务删除
#删除定时任务 #删除定时任务
croncmd -l > "$TMPDIR"/cron cronset "$1"
sed -i "/$1/d" "$TMPDIR"/cron && croncmd "$TMPDIR"/cron
rm -f "$TMPDIR"/cron
#删除条件任务 #删除条件任务
sed -i "/$1/d" "$CRASHDIR"/task/cron 2>/dev/null sed -i "/$1/d" "$CRASHDIR"/task/cron 2>/dev/null
sed -i "/$1/d" "$CRASHDIR"/task/bfstart 2>/dev/null sed -i "/$1/d" "$CRASHDIR"/task/bfstart 2>/dev/null
@@ -214,7 +212,7 @@ task_type(){ #任务条件选择菜单
task_manager(){ #任务管理列表 task_manager(){ #任务管理列表
echo "-----------------------------------------------" echo "-----------------------------------------------"
#抽取并生成临时列表 #抽取并生成临时列表
croncmd -l > "$TMPDIR"/task_cronlist cronload > "$TMPDIR"/task_cronlist
cat "$TMPDIR"/task_cronlist "$CRASHDIR"/task/running 2>/dev/null | sort -u | grep -oE "task/task.sh .*" | awk -F ' ' '{print $2" "$3}' > "$TMPDIR"/task_list cat "$TMPDIR"/task_cronlist "$CRASHDIR"/task/running 2>/dev/null | sort -u | grep -oE "task/task.sh .*" | awk -F ' ' '{print $2" "$3}' > "$TMPDIR"/task_list
cat "$CRASHDIR"/task/bfstart "$CRASHDIR"/task/afstart "$CRASHDIR"/task/affirewall 2>/dev/null | awk -F ' ' '{print $2" "$3}' >> "$TMPDIR"/task_list cat "$CRASHDIR"/task/bfstart "$CRASHDIR"/task/afstart "$CRASHDIR"/task/affirewall 2>/dev/null | awk -F ' ' '{print $2" "$3}' >> "$TMPDIR"/task_list
cat "$TMPDIR"/task_cronlist 2>/dev/null | sort -u | grep -oE " #.*" | grep -v "守护" | awk -F '#' '{print "0 旧版任务-"$2}' >> "$TMPDIR"/task_list cat "$TMPDIR"/task_cronlist 2>/dev/null | sort -u | grep -oE " #.*" | grep -v "守护" | awk -F '#' '{print "0 旧版任务-"$2}' >> "$TMPDIR"/task_list
@@ -254,9 +252,8 @@ task_manager(){ #任务管理列表
read -p "旧版任务不支持管理,是否移除?(1/0) > " res read -p "旧版任务不支持管理,是否移除?(1/0) > " res
[ "$res" = 1 ] && { [ "$res" = 1 ] && {
cronname=$(echo $task_txt | awk -F '-' '{print $2}') cronname=$(echo $task_txt | awk -F '-' '{print $2}')
croncmd -l > $TMPDIR/conf && sed -i "/$cronname/d" $TMPDIR/conf && croncmd $TMPDIR/conf cronset "$cronname"
sed -i "/$cronname/d" $clashdir/tools/cron 2>/dev/null sed -i "/$cronname/d" "$CRASHDIR"/task/cron 2>/dev/null
rm -f $TMPDIR/conf
} }
else else
task_des=$(echo $task_txt | awk '{print $2}') task_des=$(echo $task_txt | awk '{print $2}')

View File

@@ -2,7 +2,6 @@
. "$CRASHDIR"/libs/set_cron.sh . "$CRASHDIR"/libs/set_cron.sh
bot_tg_start(){ bot_tg_start(){
bot_tg_stop
. "$CRASHDIR"/starts/start_legacy.sh . "$CRASHDIR"/starts/start_legacy.sh
start_legacy "$CRASHDIR/menus/bot_tg.sh" 'bot_tg' start_legacy "$CRASHDIR/menus/bot_tg.sh" 'bot_tg'
bot_tg_cron bot_tg_cron
@@ -14,6 +13,5 @@ bot_tg_stop(){
rm -f "$TMPDIR/bot_tg.pid" rm -f "$TMPDIR/bot_tg.pid"
} }
bot_tg_cron(){ bot_tg_cron(){
cronset 'TG_BOT守护进程'
cronset 'TG_BOT守护进程' "* * * * * /bin/sh $CRASHDIR/starts/start_legacy_wd.sh bot_tg #ShellCrash-TG_BOT守护进程" cronset 'TG_BOT守护进程' "* * * * * /bin/sh $CRASHDIR/starts/start_legacy_wd.sh bot_tg #ShellCrash-TG_BOT守护进程"
} }

View File

@@ -33,14 +33,12 @@ if [ -n "$test" -o -n "$(pidof CrashCore)" ]; then
sleep 5 sleep 5
logger ShellCrash服务已启动 logger ShellCrash服务已启动
} & } &
ckcmd mtd_storage.sh && mtd_storage.sh save >/dev/null 2>&1 & #Padavan保存/etc/storage ckcmd mtd_storage.sh && mtd_storage.sh save >/dev/null 2>&1 #Padavan保存/etc/storage
#加载定时任务 #加载定时任务
[ -s "$CRASHDIR"/task/cron ] && croncmd "$CRASHDIR"/task/cron [ -s "$CRASHDIR"/task/cron ] && cronadd "$CRASHDIR"/task/cron
[ -s "$CRASHDIR"/task/running ] && { [ -s "$CRASHDIR"/task/running ] && {
cronset '运行时每' cronset '运行时每'
while read line; do cronadd "$CRASHDIR"/task/running
cronset '2fjdi124dd12s' "$line"
done <"$CRASHDIR"/task/running
} }
[ "$start_old" = "ON" ] && cronset '保守模式守护进程' "* * * * * /bin/sh $CRASHDIR/starts/start_legacy_wd.sh shellcrash #ShellCrash保守模式守护进程" [ "$start_old" = "ON" ] && cronset '保守模式守护进程' "* * * * * /bin/sh $CRASHDIR/starts/start_legacy_wd.sh shellcrash #ShellCrash保守模式守护进程"
#加载条件任务 #加载条件任务
@@ -48,7 +46,7 @@ if [ -n "$test" -o -n "$(pidof CrashCore)" ]; then
[ -s "$CRASHDIR"/task/affirewall -a -s /etc/init.d/firewall -a ! -f /etc/init.d/firewall.bak ] && { [ -s "$CRASHDIR"/task/affirewall -a -s /etc/init.d/firewall -a ! -f /etc/init.d/firewall.bak ] && {
#注入防火墙 #注入防火墙
line=$(grep -En "fw.* restart" /etc/init.d/firewall | cut -d ":" -f 1) line=$(grep -En "fw.* restart" /etc/init.d/firewall | cut -d ":" -f 1)
sed -i.bak "${line}a\\. "$CRASHDIR"/task/affirewall" /etc/init.d/firewall sed -i.bak "${line}a\\. $CRASHDIR/task/affirewall" /etc/init.d/firewall
line=$(grep -En "fw.* start" /etc/init.d/firewall | cut -d ":" -f 1) line=$(grep -En "fw.* start" /etc/init.d/firewall | cut -d ":" -f 1)
sed -i "${line}a\\. $CRASHDIR/task/affirewall" /etc/init.d/firewall sed -i "${line}a\\. $CRASHDIR/task/affirewall" /etc/init.d/firewall
} & } &

View File

@@ -4,7 +4,7 @@ PIDFILE="/tmp/ShellCrash/$1.pid"
if [ -f "$PIDFILE" ]; then if [ -f "$PIDFILE" ]; then
PID="$(cat "$PIDFILE")" PID="$(cat "$PIDFILE")"
if [ -n "$PID" ] && kill -0 "$PID" 2>/dev/null; then if [ -n "$PID" ] && [ -d "/proc/$PID" ]; then
return 0 return 0
fi fi
fi fi