Can't Run Mosquitto Commands in PHP


Can't Run Mosquitto Commands in PHP



I cannot run the mosquitto_sub or pub commands through the PHP system(), exec(), shell_exec(), etc.



Other commands (like hostname and ls) will echo and work fine. Is there something I'm missing?



Here's what I have:


<?php

$message = '{"state":{"desired":{"LED":"ON"}}}';
$cmd = "mosquitto_pub --cafile /var/www/subsystem/crt/rootCA.pem --cert
/var/www/subsystem/crt/crt.pem --key /var/www/subsystem/crt/private.pem -h
myendpoint.amazonaws.com -p 8883 -t '" . "$" .
"aws/things/LED/shadow/update' -m '" . $message . "'";

echo system($cmd);

?>



(I know that the string won't parse right with the indenting, and in my PHP file the $cmd variable is a single line.)



PS: I've also tried echoing $cmd and copy/pasting that straight to shell, and it works unaltered.



Thanks!





What's the output of the command? Also add a second parameter to the system($cmd, $exitCode); to see what exit value you get from the command. How are you executing the script: from the CLI or served via HTTP? Does the user running the script have the required permissions to run that command?
– Bogdan
Jun 30 at 12:59


system($cmd, $exitCode);





Thanks for the quick reply. I added an error variable like you suggested, but it echos nothing :(. The user for apache is apache, and is a pseudo-user, in that I can't actually 'use' the user moreorless it just runs the daemon. I do execute the mosquitto_pub script via commandline with no issues. How could I give the apache user access to the mosquitto commands?
– Timothy Martin
Jun 30 at 13:09





Update: you were correct in your assumption that this is a permissions problem. Tacking on "2>&1" on the end of my variable gave me an output this time: "Warning: Unable to locate configuration directory, default config not loaded. Error: Permission denied Error: Permission denied". How could I fix this?
– Timothy Martin
Jun 30 at 13:15





mosquitto_sub will never return unless you you use the -C]
– hardillb
Jun 30 at 17:26


-C]




1 Answer
1



mosquitto_sub will never return unless you you use the -C <number> to tell it to quit after messages. If there are no messages published on the topic given then it will never return.


-C <number>



There are real php MQTT client libraries that would be better suited to what you are trying to do e.g. https://github.com/mqtt/mqtt.github.io/wiki/libraries#php



mosquitto_pub doesn't return anything so there will be nothing to print out.





Thanks for the answer. Still wrapping my head around the complexity of AWS iot shadows, not your everyday mqtt lol.
– Timothy Martin
Jun 30 at 21:05






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to input without newline? (Python)

C++ thread error: no type named ‘type’ MINGW

Analog for TagView in flutter