1

I cannot figure out, how to pass parameters to PHP exec function that I use to run scripts via website.

After some googling I ended up with this:

exec("nohup sudo python /home/pi/Python/RGB_Controller/write_color.py ".$HEX." &", $status);

print_r($status);

This script simply takes the parameter and writes it into .txt file (if there is parameter). When I run it via bash it works as expected. But when I run it with exec the file is not changed.

Running scripts without parameters works fine. This also does not work:

exec("nohup sudo python /home/pi/Python/RGB_Controller/write_color.py test &", $status);

EDIT: Dammit. So the problem was elsewhere. In my python script that writes the file I had only relative filepath for my file. So it worked probably many times when I tried. Only the data.txt file was being created in the server root directory and thus never updating the one in my Python folder.

5
  • I think you might need to escape the ampersand - you can use escapeshellcmd()
    – calcinai
    Oct 26, 2016 at 14:00
  • @calcinai but exec without parameters and with ampersand works fine..
    – Filip
    Oct 26, 2016 at 22:04
  • Ah, sorry - I was suggesting that to avoid forking the process, which I now see is intended behavior.
    – calcinai
    Oct 27, 2016 at 6:02
  • Just tried it now without nohup and & but nothing changed.
    – Filip
    Oct 27, 2016 at 6:12
  • I know it might be overkill, but have you had a look at something like github.com/symfony/process? A library like that would allow much more graceful handling of the process. As a side note, what are you controlling with the python script? I currently maintain a php raspberry pi library and could build it in if it's a common device.
    – calcinai
    Oct 27, 2016 at 6:28

1 Answer 1

1

Can you try something like the below code snippet.

private function runCom(){
    $command = 'nohup sudo python /home/pi/Python/RGB_Controller/write_color.py' .$HEX.'& > /dev/null 2>&1 & echo $!';
    exec($command ,$op);
    $this->pid = (int)$op[0];
}

Its like you build your command with necessary arguments as a string.

Then exec($command ,$op). Please check and let me know.

5
  • Thanks! Will try it as soon as I get home (this Thursday).
    – Filip
    Oct 25, 2016 at 12:00
  • I don't get this :/ It again does not work. When I print the command it looks fine and work locally. I just needed to modify spaces before and after .$HEX. variable in your code. Which of course also didn't help.
    – Filip
    Oct 26, 2016 at 22:03
  • @Filip I will update you by today once I am back home.
    – Varad A G
    Oct 27, 2016 at 6:27
  • 1
    The problem was elsewhere. See my edit.
    – Filip
    Oct 27, 2016 at 7:09
  • 1
    @Filip Glad to hear that it worked for you.
    – Varad A G
    Oct 27, 2016 at 9:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.