Boot Sequence and Priority

I am wondering if someone can shed some light on how priority works in OpenWrt boot sequence that is found in System -> Startup tab. It starts with 0 and goes to 99 but if it's bigger than 99 then the script moves back up indicating that only the first 2 digits get recognized by it.

On the other hand I would like information on the priority itself. How does OpenWrt start those scripts, is it some kind of delay tactic, so specific scripts start at the specific time and not before that? How does it calculate what time to start the script? How does it calculate the delay between the scripts?

I would really appreciate if someone can answer these questions, thank you.

The scripts in init-style start/stop directories are typically executed with run-parts or a script equivalent that executes them in "shell-glob" order (alphabetical). Hence the reason that you may see 05something.sh as otherwise 10something_else.sh would be executed first.

In the init script there are two numbers, one for start up, one for shutdown. This allows one to modify the "reverse" the order as needed for shutdown (it's not always exactly the reverse that is needed). In OpenQrt you'll find the enabled services symlinked in /etc/rc.d with either an S or a K to indicate start or kill ordering.

Scripts are typically run sequentially and a "failure" return status from any of them may end the start-up or shut-down sequence unexpectedly. If you're writing your own, think about what will happen if your script fails somehow -- if it hangs (or runs) forever, your machine will never complete boot and you may not be able to log in, as SSH may not be running yet.

1 Like

I am not interested in writing my own script but I am more interested in the priority numbering. Because I thought the priority is more like a start time in seconds from boot (but I think it's wrong). For instance, if cron is given a priority of 30 then it would start 30 seconds after the router initially boots up? I don't want to sound stupid but I was thinking maybe there is a way to make a certain script start after a certain time. For example wait a few seconds to let the device connect to internet and then start the vpn-policy-routing. I know I can always put sleep command in /etc/rc.local and then start it from there. But I thought maybe there is a built-in way for that. Please correct me if I'm mistaken anywhere.

No, it is simple ordering, not time.

If you want to start something NN seconds after an event in the boot sequence, you'll need to start something at the "from now point" (otherwise the boot sequence will "freeze" for NN seconds), that will later start what you want.

Be aware that time changes quite a bit during the boot sequence (once early, based on file system stamps, as there is typically not a battery-backed real-time clock, once when NTP is up and running), typical tools like cron or at are not robust.

For most things, using a "hotplug" script -- one triggered on an event happening, such as NTP sync or DHCP lease granted -- is better than a fixed delay. Like start-up scripts, these scripts should also be "quick and fail-proof" in nature, though they can, for example start daemons.

Using sleep (of more than a very brief time) in a start-up, shut-down, hotplug, or other system script is pretty much always a bad idea, due to the serial nature of execution, including scripts and actions that might not be immediately apparent to you.

Alright, got it now. Thanks.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.