How to run a non-service command at boot before rc.local

I have a command I want to run on every boot — it just sends an I2C command and exits. /etc/rc.local runs far too late in the boot process to put it in there (rc.local runs at S95 and I want it more like S10). I can't do this with procd because it expects the process to keep running indefinitely. Besides, it seems like it's not the sort of thing that procd or the init.d scripts are designed for.

Is there some way to do this, or at least shoehorn it into procd/init.d?

1 Like
cat << "EOF" > /etc/init.d/custom
#!/bin/sh /etc/rc.common
START=15
boot() {
    if [ -f /etc/rc.custom ]
    then sh /etc/rc.custom
    fi
}
EOF
chmod +x /etc/init.d/custom
/etc/init.d/custom enable

https://openwrt.org/docs/techref/initscripts

2 Likes

Or alternatively, you might have the command in a uci-defaults file.

Normally uci-defaults are deleted automatically after first successful completion, but that is controlled by the script exit code. Setting an error-like exit code (non-zero) would leave the script intact for the next boot.

https://openwrt.org/docs/guide-user/base-system/uci#uci_defaults

2 Likes

I read about the boot() function on that page, but didn't really understand what it was saying. I'll try it out and see if that does what I need. Thanks!

1 Like

Thanks @vgaetera - I had another look at rc.common which contains the functions init scripts are meant to shadow. It answered a couple of lingering questions I had such as:

  • there is also a shutdown() function, called on shutdown or reboot
  • both boot() and shutdown() can be used in a procd style init script too
  • your own override of boot() should probably call start "$@" at the end, and shutdown() should probably call stop (no args) at the start.
1 Like

I updated the wiki's init scripts page as well as a few other places that point to it to include a bit more information on this. Let me know if anything's unclear.

3 Likes

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