diff --git a/scripts/libs/set_cron.sh b/scripts/libs/set_cron.sh index a38feb0a..e74cc18d 100644 --- a/scripts/libs/set_cron.sh +++ b/scripts/libs/set_cron.sh @@ -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="/var/spool/cron/crontabs" [ ! -w "$crondir" ] && crondir="/var/spool/cron" +[ -z "$USER" ] && USER=$(whoami 2>/dev/null) tmpcron=/tmp/cron_tmp touch "$tmpcron" -croncmd() { #定时任务工具 - if [ -w "$crondir" ] && [ -n "$USER" ];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 +cronadd() { #定时任务工具 + if crontab -h 2>&1 | grep -q '\-l'; then 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 echo "找不到可用的crond或者crontab应用!No available crond or crontab application can be found!" fi } cronset() { #定时任务设置 # 参数1代表要移除的关键字,参数2代表要添加的任务语句 - croncmd -l >"$tmpcron" - sed -i "/$1/d" "$tmpcron" - echo "$2" >>"$tmpcron" - croncmd "$tmpcron" - rm -f "$tmpcron" + cronload | grep -v '^$' | grep -vF "$1" >"$tmpcron" + [ -n "$2" ] && echo "$2" >>"$tmpcron" + cronadd "$tmpcron" + #华硕/Padavan固件存档在本地,其他则删除 + if [ -d /jffs ] || [ -d /etc/storage/ShellCrash ];then + mv -f "$tmpcron" "$CRASHDIR"/task/cron + else + rm -f "$tmpcron" + fi + sleep 1 } diff --git a/scripts/menus/5_task.sh b/scripts/menus/5_task.sh index 6044db97..5886de0a 100644 --- a/scripts/menus/5_task.sh +++ b/scripts/menus/5_task.sh @@ -108,9 +108,7 @@ task_add(){ #任务添加 } task_del(){ #任务删除 #删除定时任务 - croncmd -l > "$TMPDIR"/cron - sed -i "/$1/d" "$TMPDIR"/cron && croncmd "$TMPDIR"/cron - rm -f "$TMPDIR"/cron + cronset "$1" #删除条件任务 sed -i "/$1/d" "$CRASHDIR"/task/cron 2>/dev/null sed -i "/$1/d" "$CRASHDIR"/task/bfstart 2>/dev/null @@ -214,7 +212,7 @@ task_type(){ #任务条件选择菜单 task_manager(){ #任务管理列表 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 "$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 @@ -254,9 +252,8 @@ task_manager(){ #任务管理列表 read -p "旧版任务不支持管理,是否移除?(1/0) > " res [ "$res" = 1 ] && { cronname=$(echo $task_txt | awk -F '-' '{print $2}') - croncmd -l > $TMPDIR/conf && sed -i "/$cronname/d" $TMPDIR/conf && croncmd $TMPDIR/conf - sed -i "/$cronname/d" $clashdir/tools/cron 2>/dev/null - rm -f $TMPDIR/conf + cronset "$cronname" + sed -i "/$cronname/d" "$CRASHDIR"/task/cron 2>/dev/null } else task_des=$(echo $task_txt | awk '{print $2}') diff --git a/scripts/menus/bot_tg_service.sh b/scripts/menus/bot_tg_service.sh index 62ede38b..d686d337 100644 --- a/scripts/menus/bot_tg_service.sh +++ b/scripts/menus/bot_tg_service.sh @@ -2,7 +2,6 @@ . "$CRASHDIR"/libs/set_cron.sh bot_tg_start(){ - bot_tg_stop . "$CRASHDIR"/starts/start_legacy.sh start_legacy "$CRASHDIR/menus/bot_tg.sh" 'bot_tg' bot_tg_cron @@ -14,6 +13,5 @@ bot_tg_stop(){ rm -f "$TMPDIR/bot_tg.pid" } bot_tg_cron(){ - cronset 'TG_BOT守护进程' cronset 'TG_BOT守护进程' "* * * * * /bin/sh $CRASHDIR/starts/start_legacy_wd.sh bot_tg #ShellCrash-TG_BOT守护进程" } diff --git a/scripts/starts/afstart.sh b/scripts/starts/afstart.sh index 504289e6..6a493e63 100644 --- a/scripts/starts/afstart.sh +++ b/scripts/starts/afstart.sh @@ -33,14 +33,12 @@ if [ -n "$test" -o -n "$(pidof CrashCore)" ]; then sleep 5 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 ] && { cronset '运行时每' - while read line; do - cronset '2fjdi124dd12s' "$line" - done <"$CRASHDIR"/task/running + cronadd "$CRASHDIR"/task/running } [ "$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 ] && { #注入防火墙 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) sed -i "${line}a\\. $CRASHDIR/task/affirewall" /etc/init.d/firewall } & diff --git a/scripts/starts/start_legacy_wd.sh b/scripts/starts/start_legacy_wd.sh index 277fa838..281540d1 100644 --- a/scripts/starts/start_legacy_wd.sh +++ b/scripts/starts/start_legacy_wd.sh @@ -4,7 +4,7 @@ PIDFILE="/tmp/ShellCrash/$1.pid" if [ -f "$PIDFILE" ]; then PID="$(cat "$PIDFILE")" - if [ -n "$PID" ] && kill -0 "$PID" 2>/dev/null; then + if [ -n "$PID" ] && [ -d "/proc/$PID" ]; then return 0 fi fi