Mqtt service command is not working via C, Lua

i've installed mqtt at my router and uses WEB UI to control this.
here are some lines that i added into mosquitto.conf

user root
allow_anonymous false
password_file /etc/mosquitto/passwd

here are some part of the lua script.

if mqttOnOff == "1" then // mqttOnOff is setted at WEB UI.
    os.execute("service mosquitto enable")
    os.execute("echo \"mosquitto service is enabled\" > /dev/console")
    os.execute("service mosquitto stop")
    os.execute("echo \"mosquitto service is stopped\" > /dev/console")
    os.execute("service mosquitto start")
    os.execute("echo \"mosquitto service is started\" > /dev/console")
elseif mqttOnOff == "0" then
    os.execute("service mosquitto disable")
    os.execute("echo \"mosquitto service is disabled\" > /dev/console")
    os.execute("service mosquitto stop")
    os.execute("echo \"mosquitto service is stopped\" > /dev/console")
    os.execute("echo \"mqttOnOff value returned with error\" > /dev/console")

whenever i set mqttOnOff's value to 1, i can see following lines at console

mosquitto service is enabled
mosquitto service is stopped
mosquitto service is started

if i set value to 0, obviously i can see...

mosquitto service is disabled
mosquitto service is stopped

anyways, this means service commands are being executed with os.execute()
However, it doesn't work.

whenever i manually input those commands at console with my fingers, it works. it stops well, it starts well, it restarts well, it enables well, it disables well.

but everytime i use lua's os.execute doesn't work.

i manually start mosquitto and set with WEB UI to enable(start)again and check the PID with ps, PID shows same(should be different since command has serevice mosquitto stop and start). if i set with WEB UI to disable(start), it does not stops.

Manually stopping mosquitto and set with WEB UI to enable, does not starts.

So, i used C, instead of lua. Mechanism is same. just using system() function since it's C.

if (mqttOnOff == 1)
    system("service mosquitto enable");
    system("echo enable > /dev/console");
    system("service mosquitto stop");
    system("echo stop > /dev/console");
    system("service mosquitto start");
    system("echo start > /dev/console");
else if (mqttOnOff == 0)
    system("service mosquitto disable");
    system("echo disable > /dev/console");
    system("service mosquitto stop");
    system("echo stop > /dev/console");

Same as the lua, i can see


when i set via WEB UI, even if manually execute this compiled C file.

Result is the same. mqtt is not starting.

any solutions for this?

The "service" command is not an executable, you cannot invoke it that way. Easiest solution is to call the script at "/etc/init.d/..." instead.

1 Like

i figured out exact thing you told be before 1 hour ago and it worked.
still thanks a lot!!!

1 Like

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