I want to insert a startup script for Merlin custom scripts. Do I just need to add the script to the next line of "/jffs/scripts/init-start"? For example, /jffs/scripts/set_crontab.sh. Thank you everyone!
I'm attempting to execute a custom cron job path at startup, which will then trigger the execution of restart_wan.sh and /ss_rule_update.sh. I'll post the detailed code for several files for review. Thank you for your assistance.
1、init-start
2、restart_wan.sh
3、set_crontab.sh
4、"/jffs/scripts/crontabs/klcb2010",cron job not working
I'm attempting to execute a custom cron job path at startup, which will then trigger the execution of restart_wan.sh and /ss_rule_update.sh. I'll post the detailed code for several files for review. Thank you for your assistance.
1、init-start
Code:
#!/bin/sh
/jffs/scripts/set_crontab.sh
2、restart_wan.sh
Code:
#!/bin/bash
# /jffs/scripts/restart_wan.sh
# 设置日志文件路径(与脚本同目录)
LOG_FILE="/jffs/scripts/restart_wan.log"
# 获取当前日期的日部分
CURRENT_DAY=$(date +"%d")
# 检查当前日期是否为偶数(能被2整除)
if [ $((CURRENT_DAY % 2)) -eq 0 ]; then
# 如果是偶数日期,则写入开始执行的日志
echo "$(date) - Starting restart_wan.sh on even day" >> "$LOG_FILE"
# 执行重启命令,并将输出也记录到日志中
/sbin/service restart_wan >> "$LOG_FILE" 2>&1
# 检查上一个命令的退出状态,并在日志中记录
if [ $? -eq 0 ]; then
echo "$(date) - Service restart_wan restarted successfully on even day" >> "$LOG_FILE"
else
echo "$(date) - Failed to restart service restart_wan on even day" >> "$LOG_FILE"
fi
# 写入执行结束的日志
echo "$(date) - Finished restart_wan.sh on even day" >> "$LOG_FILE"
else
# 如果不是偶数日期,则记录日志但不执行重启
echo "$(date) - Skipping restart_wan.sh on odd day" >> "$LOG_FILE"
fi
Code:
#!/bin/sh
# 设置要添加 cron 任务的用户名
USER="klcb2010"
# 定义日志文件路径和名称
LOG_FILE="/jffs/scripts/set_crontab.log"
# 记录启动日志
echo "$(date): set_crontab.sh 开始执行" >> "$LOG_FILE"
# 从自定义文件读取 cron 任务
CRON_FILE="/jffs/scripts/crontabs/klcb2010"
# 检查文件是否存在
if [ -f "$CRON_FILE" ]; then
# 使用 crontab 命令设置用户的 crontab
(crontab -l -u $USER; echo "$(cat $CRON_FILE)") | crontab -u $USER -
# 记录成功更新 cron 任务的日志
echo "$(date): Cron tasks for $USER have been updated from $CRON_FILE" >> "$LOG_FILE"
else
# 记录文件不存在的错误日志
echo "$(date): Cron tasks file $CRON_FILE does not exist!" >> "$LOG_FILE"
fi
# 记录结束日志
echo "$(date): set_crontab.sh 执行完成" >> "$LOG_FILE"
4、"/jffs/scripts/crontabs/klcb2010",cron job not working
Code:
0 5 * * * /jffs/scripts/restart_wan.sh #wan_reboot#
0 4 * * * /bin/sh /koolshare/scripts/ss_rule_update.sh #update_ss #
Last edited: