What's new
  • 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!

How to Detect when Firmware is Updating via Script

copperhead

Regular Contributor
I need to pause a network monitor script during firmware updates to avoid interference. Is there a way for scripts to detect when a firmware update is being inspected / applied by the front-end UI?
 
I need to pause a network monitor script during firmware updates to avoid interference. Is there a way for scripts to detect when a firmware update is being inspected / applied by the front-end UI?

Is the "network monitoring" script running on the router or elsewhere like on a server? If it's on the router it's much easier. How is it being triggered to run? Via cron job?
For example, MerlinAU stops all other cron jobs from other scripts and Entware processeses such as unbound and diversion, etc before unmounting the USB and starting the flash.
 
Is the "network monitoring" script running on the router or elsewhere like on a server? If it's on the router it's much easier. How is it being triggered to run? Via cron job?
For example, MerlinAU stops all other cron jobs from other scripts and Entware processeses such as unbound and diversion, etc before unmounting the USB and starting the flash.

The internet monitoring script is running on the router. It is a python script which was launched by the post-mount event. The script remains active indefinitely checking the internet every 30 seconds in the following order:

1. Check Internet:
x Performs ping test
x Checks DNS resolution

** If any of the above fail then ....

2. Try to Fix Internet connectivity. The script runs through each of the following checking the internet status at each level. If the internet is restored, the function is exited and the script goes back to Sleep for 30 secs then, when awake, restarts at (1) - Check Internet:
x Attempts DNS fix.
x Renews ISP DHCP lease
x Restarts WAN interface
x Restarts router
x Restarts modem

My concern has to do with when the router is checking / applying its firmware. The last time i updated the firmware, I've received notifications from the script that the internet wasn't working. It got all the way down to "Restart WAN Interface". Thankfully it never got to the "Restart Router" stage which, to me, seems like a bad idea when firmware updating.
 
The internet monitoring script is running on the router. It is a python script which was launched by the post-mount event. The script remains active indefinitely checking the internet every 30 seconds in the following order:

1. Check Internet:
x Performs ping test
x Checks DNS resolution

** If any of the above fail then ....

2. Try to Fix Internet connectivity. The script runs through each of the following checking the internet status at each level. If the internet is restored, the function is exited and the script goes back to Sleep for 30 secs then, when awake, restarts at (1) - Check Internet:
x Attempts DNS fix.
x Renews ISP DHCP lease
x Restarts WAN interface
x Restarts router
x Restarts modem

My concern has to do with when the router is checking / applying its firmware. The last time i updated the firmware, I've received notifications from the script that the internet wasn't working. It got all the way down to "Restart WAN Interface". Thankfully it never got to the "Restart Router" stage which, to me, seems like a bad idea when firmware updating.

Well to answer your question; the "easiest" identifier for example would be in the system logs:

Code:
May  5 09:05:31 custom_script: Running /jffs/scripts/service-event (args: start upgrade)
May  5 09:05:30 rc_service: httpd 4893:notify_rc start_upgrade

Which can be identified with this shell command:
Code:
grep -i "start_upgrade" /tmp/syslog.log

1746451890735.png


In the case of MerlinAU with your python script, as long as the script is installed on the USB the process would be shutdown by MerlinAU's USB unmount or the entware unslung command (not sure where python is installed)
 
Last edited:
The internet monitoring script is running on the router. It is a python script which was launched by the post-mount event. The script remains active indefinitely checking the internet every 30 seconds in the following order:

1. Check Internet:
x Performs ping test
x Checks DNS resolution

** If any of the above fail then ....

2. Try to Fix Internet connectivity. The script runs through each of the following checking the internet status at each level. If the internet is restored, the function is exited and the script goes back to Sleep for 30 secs then, when awake, restarts at (1) - Check Internet:
x Attempts DNS fix.
x Renews ISP DHCP lease
x Restarts WAN interface
x Restarts router
x Restarts modem

My concern has to do with when the router is checking / applying its firmware. The last time i updated the firmware, I've received notifications from the script that the internet wasn't working. It got all the way down to "Restart WAN Interface". Thankfully it never got to the "Restart Router" stage which, to me, seems like a bad idea when firmware updating.

There is an even earlier process which is this one:
May 5 09:05:23 custom_script: Running /jffs/scripts/service-event (args: stop upgrade)

Which means when you start an upgrade, the service-event script is triggered with the event parameters: stop upgrade
You can piggy back on this if you'd like. when the service-event receives the "stop" and "upgrade" parameters; it can shutdown your python script.
 
Last edited:
There is an even earlier process which is this one:
May 5 09:05:23 custom_script: Running /jffs/scripts/service-event (args: stop upgrade)

Which means when you start an upgrade, the service-event script is triggered with the event parameters: stop upgrade
You can piggy back on this if you'd like. when the service-event receives the "stop" and "upgrade" parameters; it can shutdown your python script.

That is perfect!

So i would use the service-event: stop upgrade to stop my python script. Got it. Not that i need it but out of curiosity, what event is issued when the upgrade is complete? Dont tell me it is service-event: start upgrade. That would really mess with my mind.
 
That is perfect!

So i would use the service-event: stop upgrade to stop my python script. Got it. Not that i need it but out of curiosity, what event is issued when the upgrade is complete? Dont tell me it is service-event: start upgrade. That would really mess with my mind.

Look inside the service_event script for existing examples; here is the one MerlinAU uses:

Code:
if echo "$2" | /bin/grep -q "MerlinAU" ; then { /jffs/scripts/MerlinAU.sh service_event "$@" & }; fi #MerlinAU#

So if the second parameter is "MerlinAU" then it passes all the original variables to the MerlinAU script when calling it.

But for your example, you would likely want to do something like:
Code:
if [ "$1" = "stop" ] && [ "$2" = "upgrade" ]; then
    PIDS="$(ps | grep '[p]ython .*/Test/test\.py' | awk '{print $1}')"
    if [ -n "$PIDS" ]; then
        # Graceful SIGTERM
        kill $PIDS 2>/dev/null
        sleep 2
        # Force‑kill any survivor
        for PID in $PIDS; do
            if kill -0 "$PID" 2>/dev/null; then
                kill -9 "$PID" 2>/dev/null
            fi
        done
    fi
fi

Remember that it's being passed as 2 parameters, one being "stop" or "start" and the other being "upgrade".
Think of it as the event calling the service-event script is the "upgrade" event. Why? Because It's "stopping" the services for the "upgrade" event.

It gets called again shortly after when the actual upgrade starts, but very clearly after the "stop" is issued if you look at the time stamps:

Code:
May 5 09:05:23 custom_script: Running /jffs/scripts/service-event (args: stop upgrade)
May  5 09:05:31 custom_script: Running /jffs/scripts/service-event (args: start upgrade)

For your last question; the service-event is not triggered again once the upgrade is completed.
The router simply reboots. The next time you would see the service-event being triggered is post reboot when restarting the services.
 
Fantastic explanation. I have it all implemented now and verified. Much appreciated!
 
Fantastic explanation. I have it all implemented now and verified. Much appreciated!

Happy to hear! And glad I could help point you in the right direction.
 

Similar 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!
Back
Top