OpenWrt Forum Archive

Topic: Is This a Working Diag/Button Script for RC6

The content of this topic has been archived on 29 Mar 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

I've never really written shell scripts before, so tell me if this makes sense, probably badly hacked together from original LED script from RC5. Tell me if it's all logical and necessary. Supposed to make the white SES flash, the orange turn on, and then reboot the router.

mkdir /etc/hotplug.d
mkdir /etc/hotplug.d/button

touch /etc/hotplug.d/button/script
echo '#!/bin/sh

while : ; do
  sleep 1
  # Cisco button
  if [ $BUTTON = "SES" && $ACTION = "Pressed" ]; then
    logger "Rebooting (Reset button)"
      echo "f" > /proc/diag/led/ses_white
      echo "1" > /proc/diag/led/ses_orange
    reboot
  fi
done &' > /etc/hotplug.d/button/script

chmod a+x /etc/hotplug.d/button/script

(Last edited by napierzaza on 13 Nov 2006, 22:47)

Don't use a loop like that. The script is called once for every button action...

You're right, I think I have everything correct now with the following script. I made a test script to make sure the variables were coming in properly. Though one note: my test script echoed ">>" into a text file, and the result was about a hundred pairs of this:

action: released
button: ses
seen: 0

action: pressed
button: ses
seen: 233

I'm pretty certain that I only pressed it once so I'm not sure why it echoed that so many times. Hopefully that won't screw up my script.

echo '#!/bin/sh
    # Cisco button
    if [ $BUTTON = "ses"]; then
        if [ $ACTION = "pressed"]; then
            logger "Rebooting (Reset button)"
              echo "f" > /proc/diag/led/ses_white
              echo "1" > /proc/diag/led/ses_orange
              sleep 1
               echo "0" > /proc/diag/led/ses_white
              echo "0" > /proc/diag/led/ses_orange
            reboot
        fi
    fi' > /etc/hotplug.d/button/script

chmod +x /etc/hotplug.d/button/script

From what I've read recently about shell scripting the use of && is different so I made it nested. As you said I shouldn't use a loop, it's not a daemon anymore.

(Last edited by napierzaza on 13 Nov 2006, 23:18)

Okay figured it out and tested it, this one works:

#!/bin/sh
    # Cisco button
    if [ "$BUTTON" = "ses" ]; then
       if [ "$ACTION" = "released" ]; then
          logger "Rebooting (Reset button)"
          echo "f" > /proc/diag/led/ses_white
          echo "1" > /proc/diag/led/ses_orange
          sleep 2
          echo "0" > /proc/diag/led/ses_white
          sleep 1
          echo "0" > /proc/diag/led/ses_orange
          reboot
       fi
    fi
napierzaza wrote:

From what I've read recently about shell scripting the use of && is different so I made it nested. As you said I shouldn't use a loop, it's not a daemon anymore.

To do what you wanted you need something like:

if [ $BUTTON = "SES" -a $ACTION = "Pressed" ]; then

The [ comand is simply another name for the test command. In most systems /bin/[ is simply a link to /bin/test. Of course in embedded systems like this everything is built into the shell.

I prefer using Cisco button to toggle wireless conectivity

#!/bin/sh
# Cisco Button
if [ $BUTTON = "ses" -a $ACTION = "released" ]
then
        echo "f" > /proc/diag/led/ses_white

        if [ "$(nvram get wl0_radio)" = "0" ]
        then
                logger -t wifi "Activating wi-fi"
                nvram set wl0_radio=1
                wifi
        else
                logger -t wifi "Activating wi-fi"
                nvram set wl0_radio=0
                wifi
        fi
        sleep 1
        echo "0" > /proc/diag/led/ses_white
        echo "1" > /proc/diag/led/power

fi

napierzaza, your script works great. Using it with White Russian 0.9 on my WRTSL54GS.

I modified it a bit to kill my torrent client and cleanly unmount harddisk, before rebooting.

Also if I replace reboot with HALT, is that same as shutdown??

#!/bin/sh
    # Cisco button
     if [ "$BUTTON" = "ses" ]; then
        if [ "$ACTION" = "released" ]; then
          logger "Rebooting (Reset button)"
          echo "f" > /proc/diag/led/ses_white
          echo "1" > /proc/diag/led/ses_orange
          sleep 2
          echo "0" > /proc/diag/led/ses_white
          sleep 1
          echo "0" > /proc/diag/led/ses_orange
        cd /
        killall ctorrent
        killall screen
        swapoff /mnt/disc0_1/swap
        umount /mnt/disc0_1
        reboot
       fi

The discussion might have continued from here.