1

I need to run a python script, compiled with pyinstaller via a PHP generated webpage. I tried shell_exec(), exec() and system() without success. I regularly run the script from terminal in background using:

temperature_sensor_code > /dev/null 2>&1

I've added www-data user to sudoers. I know it's not a good way but I need it in order to send killall temperature_sensor_code command (this is works).

This is my situation:

<?php

  $run = escapeshellcmd('temperature_sensor_code > /dev/null 2>&1');
  shell_exec($run);

  header("Refresh: 0; URL=index.php");
 ?>

I've made a symlink in /usr/bin, also tried with the full path of the script with no luck.

UPDATE: to make it simpler, i've created a simple sh script run.sh and put in /var/www and make it RUN with

shell_exec("/var/www/run.sh");

this is working for me. So I put my script temperature_sensor_code in /var/www but this is not working. If I add var_dump(exec("/var/www/temperature_sensor_code/temperature_sensor_code")); gives me: string(0) ""

I think there are problems with the compiled python script because the PHP side seems to be OK.

4
  • Errors? What is expected result? Commented Dec 27, 2017 at 9:02
  • No errors to report from PHP. The python script itself works fine; basically it reads a sensor and store data in MySQL. Commented Dec 27, 2017 at 9:04
  • Tried normal exec function? Commented Dec 27, 2017 at 10:39
  • @uglypointer already tried Commented Dec 27, 2017 at 13:31

1 Answer 1

1

escapeshellcmd() does this:

Escape shell metacharacters

$run = escapeshellcmd('temperature_sensor_code > /dev/null 2>&1');
var_dump($run);
string(43) "temperature_sensor_code \> /dev/null 2\>\&1"

But you have shell metacharacters that you do want to behave as shell metacharacters:

temperature_sensor_code > /dev/null 2>&1
                        ^           ^^^^

You're also doing no troubleshooting at all:

  • You discard all command output (that's what sending it to /dev/null does)
  • You don't get the return code

I suggest to:

  • Switch to exec() and make sure you use all its arguments and not just the mandatory ones
  • Get rid of > /dev/null until you diagnose the issue
Sign up to request clarification or add additional context in comments.

1 Comment

Your suggestions allowed me to find the real problem. Not the PHP configuration but insufficient www-data permission

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.