How to trigger script execute after Save&Apply from LUCI page

Hi LUCI author:
I tried to use luci-app-example to add a new page with my LUCI21.02.
I want to trigger a back-end script execution after I click "Save&Apply" in "Example/Form View" page.
I composed a script named "example" which located in /etc/init.d/ like this:

#!/bin/sh /etc/rc.common
START=20
STOP=90
USE_PROCD=1
start_service() {
        echo "start service example" >> "/var/run/log"
}
reload_service() {
        echo "reload service example" >> "/var/run/log"
}
stop_service() {
        echo "stop service example" >> "/var/run/log"
}

And I also add a code piece to /etc/config/ucitrack like this:

config example
	option init example

The luci-app-example.json is like this:

{
  "luci-app-example": {
    "description": "Grant UCI access to LuCI app ecample",
    "read": {
      "cgi-io": [ "exec" ],    
      "ubus": {
        "file": [ "exec" ],
        "uci": [ "changes", "get" ]
      },
      "uci": [
        "example"
      ]
    },
    "write": {
      "cgi-io": [ "exec" ],
      "ubus": {
        "file": [ "exec" ],
        "uci": [ "add", "apply", "confirm", "delete", "order", "rename", "set" ]
      },
      "uci": [
        "example"
      ]
    }
  }
}

But after I click the "Save&Apply" at Example/Form View page, nothing happlen(No /var/run/log exist, it's look like my /etc/init.d/example never been executed at all.
Can you tell me why?
Besides, since there is no any document about how to compose the menu.d/XX.json and acl.d/XX.json, It's really hard for me to proceed with my development, sincerely hope you can write a guideline about this.

In your init script, add:

service_triggers() {
    procd_add_reload_trigger example
}

This will execute an /etc/init.d/example reload when LuCI commits /etc/config/example.

As for the JSON menu items, you can find some info here:

See also the post below for a general overview over the request routing flow:

1 Like

Hi Jow
Thanks for your reply.

service_triggers() {
    procd_add_reload_trigger example
}

After I tried this patch, unfortunately it did not work(I mean click Save&Apply from web page).
I also tried to use uci command line, but did not work as well.

root@OpenWrt:/etc/init.d#uci set example.first.first_option=99
root@OpenWrt:/etc/init.d#uci commit

Are you sure that "uci commit" would trigger the /etc/init.d/XXX script execution?

A uci commit on the cli does not trigger the reload mechanism (you can invoke /sbin/reload_config after uci commit for that).

A uci commit call via ubus (as used by LuCI) should trigger the reload though.

Example:

root@er-x:~# vi /etc/init.d/example
root@er-x:~# cat /etc/init.d/example 
#!/bin/sh /etc/rc.common
START=20
STOP=90
USE_PROCD=1
start_service() {
        echo "start service example" >> "/var/run/log"
}
reload_service() {
        echo "reload service example" >> "/var/run/log"
}
stop_service() {
        echo "stop service example" >> "/var/run/log"
}
service_triggers() {
	procd_add_reload_trigger example
}
root@er-x:~# chmod +x /etc/init.d/example
root@er-x:~# /etc/init.d/example enable
root@er-x:~# touch /etc/config/example
root@er-x:~# uci add example example
cfg0199f1
root@er-x:~# uci set example.@example[-1].foo=bar
root@er-x:~# uci commit example
root@er-x:~# /etc/init.d/example start
root@er-x:~# cat /var/run/log 
start service example
root@er-x:~# ubus call uci set '{ "config": "example", "section": "@example[-1]", "values": { "foo": "baz" } }'
root@er-x:~# cat /var/run/log 
start service example
root@er-x:~# ubus call uci commit '{ "config": "example" }'
root@er-x:~# cat /var/run/log 
start service example
reload service example
root@er-x:~# 

Thanks, that works.

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