What's new

Running a script after unexpected reboot.

  • 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!

fryedchikin

Regular Contributor
Hi Everyone, This is a question for all you scripting experts.

I am running 378.53 on an RT-68P and would like to know how to trigger a script after an unexpected reboot occurs.

I currently have a wan-start script to perform some actions but obviously it runs every time the router reboots. I need to move these actions to a separate script which will only execute after an unexpected reboot or loss of WAN connectivity (such as a power outage or ISP outage). If the router is intentionally rebooted manually via the UI or via a cron job I would not want the script to run.

Any help would be greatly appreciated.

Thanks!
 
Anyone have any ideas ?. Is there really no way to detect an unexpected WAN outage ?.

Even Windows is smart enough to know when it wasn't shut down properly lol :)
 
I dunno
o.png
 
Anyone have any ideas ?. Is there really no way to detect an unexpected WAN outage ?.

Even Windows is smart enough to know when it wasn't shut down properly lol :)

You can identify if wan-start was invoked as part of a normal boot of the router or because of an unexpected WAN restart request..

in init-start

Code:
#!/bin/sh

HARDWARE_MODEL=$(nvram get productid)
MYROUTER=$(nvram get computer_name)
BUILDNO=$(nvram get buildno)

/usr/bin/logger -s -t "($(basename $0))" $$ "Martineau $MYROUTER BOOT in progress... [$@]"

# NOTE: Can't use Flash drive /tmp/mnt/$MYROUTER/ 'cos it hasn't been mounted yet :-(
# 'flock' is probably a better solution rather than the 'echo'
echo $$"-"`date` > /tmp/BOOTINPROGRESS # PID will allow other scripts to identify locker

# Should be sufficient to cover BOOT process?
sleep 150

# Move Syslog to USB Flash drive
/jffs/scripts/syslog-move.sh

<snip>

rm /tmp/BOOTINPROGRESS # Clear semaphore

# Create additional sempahore to indicate unexpected BOOT (see services-stop)
echo $$"-"`date` > /tmp/DIRTYBOOT

then in wan-start

Code:
# Unexpected WAN restart?  (see services-stop)
if [ -e /tmp/DIRTYBOOT  ]; then
  # Perform 'abnormal' non-BOOT wan-start actions

fi

# True REBOOT in progress? (see init-start)
if [ -e /tmp/BOOTINPROGRESS  ]; then

  BOOTINPROGRESS="YES"
  # Perform normal BOOT actions
fi

...and in services-stop

Code:
# Clear semaphore to indicate this is a controlled SHUTDOWN/REBOOT either by the GUI or clear semaphore in CRON script
rm /tmp/DIRTYBOOT


NOTE: If init-start either prematurely removes the semaphore '/tmp/BOOTINPROGRESS' or prematurely creates the semaphore '/tmp/DIRTYBOOT' then wan-start actions may be consequently inappropriate.
 
Last edited:

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