What's new

Solved How to insert a startup script for Merlin custom scripts

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

klcb2010

Occasional Visitor
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
Code:
#!/bin/sh
/jffs/scripts/set_crontab.sh

截屏图片 (1).jpg


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
3、set_crontab.sh
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:
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
Code:
#!/bin/sh
/jffs/scripts/set_crontab.sh
Is your script working if you execute it manually ? If so, you probably are running it too early. Try adding a 30 seconds delay on the first line of the script. Also, see the wiki: https://github.com/RMerl/asuswrt-merlin.ng/wiki/User-scripts


like this ?
C++:
30 0 5 * * /jffs/scripts/restart_wan.sh #wan#
30 0 4 * * * /bin/sh /koolshare/scripts/ss_rule_update.sh #update#
 
I think @Mikii is suggesting at the top of your "init-start" script you add a command -
sleep 30

But perhaps it's better to use a "services-start" script in place of the "init-wan" as that's executed as per RMerlin -
"Called after all other system services have been started at boot."
 
I think @Mikii is suggesting at the top of your "init-start" script you add a command -
sleep 30

But perhaps it's better to use a "services-start" script in place of the "init-wan" as that's executed as per RMerlin -
Like this?need reboot system ?

The content "services-start" is written as :

#!/bin/sh
sleep 30
/jffs/scripts/set_crontab.sh


When I manually execute "set_crontab.sh", it works fine. However, it is strange that the scheduled task still does not run. The relevant code is as follows:

The content"set_crontab.sh"is written as :

#!/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"

The content"/jffs/scripts/crontabs/klcb2010"is written as:

0 5 * * * /jffs/scripts/restart_wan.sh #wan_reboot#
0 4 * * * /bin/sh /koolshare/scripts/ss_rule_update.sh #update_ss #
 
Last edited:
Like this?need reboot system ?

The content "services-start" is written as :

#!/bin/sh
sleep 30
/jffs/scripts/set_crontab.sh

Yes. But as previously suggested, first test it to ensure it executes without error -
Code:
. /jffs/scripts/services-start
edit - I think I cocked up that invocation, it should be without the "." or as -
Code:
cd /jffs/scripts
./services-start

Then try it without the sleep command as it may not be needed at the time services-start is invoked.
 
Last edited:
When I manually execute "set_crontab.sh", it works fine. However, it is strange that the scheduled task still does not run.

I know very very little about crontab, does the "command" require a shell invocation, as in -
Rich (BB code):
0 5 * * * sh /jffs/scripts/restart_wan.sh #wan_reboot#
 
had changed,and it working now。
The current challenge is that the "/jffs/scripts/set_crontab.sh" script cannot be executed at bootup. I have already edited it into the "/jffs/scripts/init-start" script, which contains the following:
Bash:
"#!/bin/sh
. /jffs/scripts/set_crontab.sh"
,manually executing "/jffs/scripts/set_crontab.sh" is not a problem
 

Attachments

  • 截屏图片.jpg
    截屏图片.jpg
    202.9 KB · Views: 22
Last edited:
As indicated, this forum does not provide support for the Koolshare firmware. It is a fork with an unknown number of changes, and therefore you should reach out to the developers of that particular firmware for assistance.
 
As indicated, this forum does not provide support for the Koolshare firmware. It is a fork with an unknown number of changes, and therefore you should reach out to the developers of that particular firmware for assistance.
get it,the cron job is working now, thx !
 

Similar threads

Latest threads

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top