- Регистрация
- 1 Мар 2015
- Сообщения
- 1,481
- Баллы
- 155
When building web applications on OpenWRT, especially for device configuration, it's essential to ensure that your service management scripts function correctly. In your case, you're trying to set up a CBI (Lua-based) interface that modifies the configuration of your application, triggers a service restart, and logs actions, but you're facing some challenges.
Understanding ucitrack and Configuration Management
In OpenWRT, the /etc/config/ucitrack file plays an important role in managing service states. Each service can define how it should respond to changes in configuration. When you added the line:
config app
option init gps
this instructs OpenWRT to track this particular application for changes, but it also requires the corresponding service initialization script to properly reference the configuration change.
Review Your Init Script
Your init script located at /etc/init.d/your_service_name seems mostly correct, but it may require some tweaks:
Improved Init Script Example
Here’s an example with some modifications:
#!/bin/sh /etc/rc.common
START=10
start() {
echo "Service starting..."
date > /root/test.txt
}
stop() {
echo "Service stopping..."
}
reload_service() {
echo "Restarting service..."
stop
start
}
reload() {
reload_service
}
Notes on the Script
Since you are using CBI to handle configurations, make sure your script is correctly set up to call the init script based on UI actions. The CBI interface should be something like this:
function write_config()
local uci = require("luci.model.uci").cursor()
uci:set("app", "init", "gps")
uci:commit("app")
os.execute("/etc/init.d/your_service_name reload")
end
Explanation of the Lua Code
To troubleshoot your setup, check the following:
Why is my service not restarting?
Ensure that your CBI script calls the service reload function, and check for errors in the syslog that may indicate permission issues or misconfiguration.
How do I check the log file for output?
You can use cat /root/test.txt or tail -f /root/test.txt in your terminal to view real-time logs to see if the service logs the timestamp correctly.
What is the correct path to my configuration?
Your configuration file should reside under /etc/config/app with correct key-value pairs managed by UCI.
Conclusion
By following the above structure and debugging tips, you should be able to properly manage your application configuration and service restarts on OpenWRT effectively. Ensure your scripts are aligned with the expected interfaces, and you should see your desired outcomes.
Understanding ucitrack and Configuration Management
In OpenWRT, the /etc/config/ucitrack file plays an important role in managing service states. Each service can define how it should respond to changes in configuration. When you added the line:
config app
option init gps
this instructs OpenWRT to track this particular application for changes, but it also requires the corresponding service initialization script to properly reference the configuration change.
Review Your Init Script
Your init script located at /etc/init.d/your_service_name seems mostly correct, but it may require some tweaks:
Improved Init Script Example
Here’s an example with some modifications:
#!/bin/sh /etc/rc.common
START=10
start() {
echo "Service starting..."
date > /root/test.txt
}
stop() {
echo "Service stopping..."
}
reload_service() {
echo "Restarting service..."
stop
start
}
reload() {
reload_service
}
Notes on the Script
- Init Functionality: Ensure that your logging redirection (i.e., writing to /root/test.txt) has the proper permissions.
- Adding reload Function: This makes it explicit for the reload command to work correctly, especially after configuration changes.
Since you are using CBI to handle configurations, make sure your script is correctly set up to call the init script based on UI actions. The CBI interface should be something like this:
function write_config()
local uci = require("luci.model.uci").cursor()
uci:set("app", "init", "gps")
uci:commit("app")
os.execute("/etc/init.d/your_service_name reload")
end
Explanation of the Lua Code
- The write_config function utilizes UCI to set a configuration option for your application.
- After committing the new configuration, it executes the reload command for your service.
- Make sure that your web interface’s save and apply button correctly links to this function to ensure changes are captured.
To troubleshoot your setup, check the following:
- Permissions: Ensure that the user under which the web server runs has permission to write to /root/test.txt.
- Log Outputs: Upon clicking the save and apply button, look for any logs generated by the service, which should appear in the terminal or syslog to verify whether your start function executes as expected.
- Restart Behavior: Confirm that the reload_service function calls stop followed by start, allowing for clean service management. If the service does not restart properly, it may not be registered as a service or properly linked to ucitrack.
Why is my service not restarting?
Ensure that your CBI script calls the service reload function, and check for errors in the syslog that may indicate permission issues or misconfiguration.
How do I check the log file for output?
You can use cat /root/test.txt or tail -f /root/test.txt in your terminal to view real-time logs to see if the service logs the timestamp correctly.
What is the correct path to my configuration?
Your configuration file should reside under /etc/config/app with correct key-value pairs managed by UCI.
Conclusion
By following the above structure and debugging tips, you should be able to properly manage your application configuration and service restarts on OpenWRT effectively. Ensure your scripts are aligned with the expected interfaces, and you should see your desired outcomes.