0

I have a Laravel app. It displays a simple view with some buttons. When a button is pressed it should call an api and access a function in my controller and execute a python script.

The problem is:

  • If I run the app in local it works
  • If I run the python script from command line it works
  • If I run the app on my debian server it does not work

The script gets executed with exec function (also tried shell_exec with same result), but it doesn't return any output, doesn't throw errors or exceptions, and return code is always 1.

exec('timeout 10 python3 ' . public_path('send_command.py') . ' -c ' . escapeshellarg($command)  . ' -s ' . escapeshellarg($serial), $output, $result);

I also tried to build the command first, print it and then execute it: it gets printed fine (if I copy and paste that in command line it works) but the result is always the same.

Also tried this but to no avail.

N.B. Currently send_command.py is inside public folder because I noticed that it treats it as the current shell directory, but this doesn't work even if the script is in the project root or inside controllers folder.

Anyone has any idea of what is going on? What's different between exec() (or shell_exec()) and the command line?

Fabio R.
  • 393
  • 3
  • 15
  • I presume a full path to python3 is necessary. Using `locate bin/python3` should give the path where python executable resides. – nice_dev Oct 25 '21 at 18:10
  • @nice_dev Just tried that `exec('timeout 10 /usr/bin/python3 ' . public_path('send_command.py') . ' -c ' . escapeshellarg($command) . ' -s ' . escapeshellarg($serial), $output, $result);` | Same result: nothing – Fabio R. Oct 25 '21 at 18:25
  • I hope you are printing something in python. Instead of `public_path()`, can you give the disk path? – nice_dev Oct 25 '21 at 18:34
  • @nice_dev Yes yes, i'm printing some info and the result that has to be handled by the rest of the php script (100% working in localhost). Just tried giving the full path to everything : `exec('/usr/bin/timeout "10" /usr/bin/python3 /var/www/app/hen-backend/send_command.py -c ' . escapeshellarg($command) . ' -s ' . escapeshellarg($serial), $output, $result);` but same as always – Fabio R. Oct 25 '21 at 18:40
  • @nice_dev just tried to `touch file.txt` and the result is the same, so it's not a problem of python but of php's exec – Fabio R. Oct 25 '21 at 18:48
  • Did you ever figure this out? – Kush Mar 31 '22 at 20:42
  • @Kush nope, in the end I lost my patience and converted the whole MQTT part (that's what I was using python for) into PHP. Caused me a bit of a headache having MQTT directly inside my Laravel app, but now it works like a charm – Fabio R. May 11 '22 at 11:14
  • I also was unable to get it working the way I wanted too. Had to resort to Laravel's library called symfony/process. – Kush Jun 20 '22 at 21:35

0 Answers0