2013年8月27日星期二

DD-WRT 固件脚本添加与修改

一.在 \fmk\rootfs\etc\config 目录下新建文件 nocat_liht.sh :

#!/bin/sh
status=`/bin/ps | /bin/grep splashd | /bin/grep -v grep | /bin/wc -l`
#取得nocat状态,必须为1,不能是0,也不能是2
    if [ $status != 1 ];
    then
    /usr/sbin/splashd
   # date >> /tmp/nocat.log #记录出错时间
   # echo "liht_nocat auto restarted" >> /tmp/nocat.log
    else
    exit
    fi

注意:需要管理员登陆linux才能修改rootfs目录,保存后记得修改文件属性为可执行。

二. 修改 \fmk\rootfs\etc\config 目录下的nocat.startup文件,添加如下内容:

if [ "$(nvram get NC_enable)" = "1" ]; then
  * * * * * root /etc/config/nocat_liht.sh
fi

 意思是,如果使用了nocat,就每分钟运行一次脚本。


nocat.sh 
Code:
#!/bin/sh 
X=1 
Y=status 

while [ $X -eq 1 ] 

do 
   sleep 1000 
   cd /tmp 
   sleep 7 
   webup=`ls status` 
     if [ "$Y" != "$webup" ]; then 
          killall wget 
          sleep 5 
          killall splashd 
          echo "nocat restarted" >> nocat.log 
          sleep 20 
          splashd 
          sleep 5 
       else 
          # echo "status deleted" >> nocat.log 
          rm /tmp/status 
     fi 

done 

192.168.2.241 is the ip of the dd-wrt on the lan side 
this check if the status nocat page is working otherwise restarts nocat. The killall comand could be a reboot command if the proccess cannot be killed (depending on the enviroment may be better...). 

I put the script in the startup. 
Code:
chmod +x /tmp/nocat.sh 
/tmp/nocat.sh &

where anyserver is a webserver containing nocat.sh, and accesible for the router 

This work for me, every 24-48 hours nocat need to be restarted. Sometimes nocat depending on global configuration may be too unstable, and the script does not help very much. 

进入 DD-WRT 网页管理界面,Administration -> Commands,贴入如下脚本Save Startup:
mkdir /tmp/myscripts
    /bin/echo ‘#!/bin/sh
    status=`/bin/ps | /bin/grep splashd | /bin/grep -v grep | /bin/wc -l`
    #echo $status

    if [ $status = 0 ];
    then
    /usr/sbin/splashd >> /tmp/nocat.log 2>&1 &
    else
    exit
    fi’ >> /tmp/myscripts/monitor_splash
    /bin/chmod +x /tmp/myscripts/monitor_splash

    /bin/echo ‘ * * * * * root /tmp/myscripts/monitor_splash’ >> /tmp/crontab

    # restart cron daemon
    stopservice cron && startservice cron