You have hard drive(s) attached to your router. You’re not sure if just yanking power is going to corrupt them.
If you issue:
halt
… or:
poweroff
… the router just reboots.
So let’s set it up for a graceful shutdown.
First, create the shutdown script:
vi /etc/prep-unplug
… press i to enter editing mode, then paste this code in:
#!/bin/sh echo "Isolating WAN traffic..." ifdown wan 2>/dev/null ifdown wan6 2>/dev/null echo "Flushing filesystems..." sleep 5 sync sleep 5 sync # Turn off the default green power light if [ -d /sys/class/leds/green:status ]; then echo 0 > /sys/class/leds/green:status/brightness 2>/dev/null fi # Configure the red status LED to flash if [ -d /sys/class/leds/red:status ]; then echo timer > /sys/class/leds/red:status/trigger 2>/dev/null echo 500 > /sys/class/leds/red:status/delay_on 2>/dev/null echo 500 > /sys/class/leds/red:status/delay_off 2>/dev/null fi echo "Stopping network services..." service uhttpd stop 2>/dev/null service dnsmasq stop 2>/dev/null echo "Red LED flashing: You may power down the router." # Enter a trap loop until the power is off. while true; do sleep 1 done
Press Esc to exit editing mode, then :wq to save and exit.
vi /etc/profile
Press i to enter editing mode, then scroll to the bottom of that file and enter:
alias poweroff='/etc/prep-unplug'
Press Esc to exit editing mode, then :wq to save and exit.
The above catches terminal commands. If you issue poweroff from the terminal, it’ll run the /etc/prep-unplug script.
But, if you have automated internal tasks or third-party cron scripts calling /sbin/poweroff explicitly, an alias won’t catch that. You can fix this by dropping a symbolic wrapper script higher up in the system path execution priority (/usr/sbin/).
vi /usr/sbin/poweroff
Press i to enter editing mode, then enter:
#!/bin/sh exec /etc/prep-unplug
Press Esc to exit editing mode, then :wq to save and exit.
When a service calls poweroff, the system searches /usr/sbin/. The /usr/sbin/poweroff script intercepts the hardware system call, and runs the /etc/prep-unplug script.
Make it all impervious to firmware updates. Under System >> Backup / Flash Firmware >> Configuration tab, add to the list:
/etc/profile /etc/prep-unplug /usr/sbin/poweroff
… then click the Save button at bottom.
Ensure the system sees it:
which poweroff
… which should return:
/sbin/poweroff
Ensure the system sees it:
type poweroff
… which should return:
poweroff is an alias for /etc/prep-unplug