3

I am trying to run a python script from php code. I have followed other threads to see what I'm doing wrong, but i can't seem to find the problem.

$out = shell_exec(escapeshellcmd("/path/to/script/here/script.py arg1 arg2"));
echo $out;

As you might ask, yes, the script:

  1. has "#!/usr/bin/env python3" added
  2. has permission to be read and executed by group www-data (from output apachectl -S)
  3. is executable

At the same time, any other non-script command works just fine with shell_exec()

4
  • Have you tried adding "python3 " at the front of the command? so "python3 '/path/script.py' args" Commented May 11, 2020 at 18:04
  • I think escapeshellcmd is escaping all the /. You don't need it unless that is coming from somewhere else. If the args are then just use escapeshellarg on those individually, maybe. Commented May 11, 2020 at 18:06
  • @AbraCadaver surely you want the / escaped, given they're just text as part of the path, rather than anything special? Anyway, isn't it \ that's usually the special one? XDead, what happens if you put the python file next to your php file so you dont need to include any slashes and can just call it by name? Commented May 11, 2020 at 18:13
  • @ch4rl1e97 I have just tried to place the script file in the same directory as the php file and the result is the same. Also adding 'python3' or '/usr/bin/python3' does nothing. Commented May 11, 2020 at 18:30

2 Answers 2

3

I tried your code, it is working fine for me. If there is some error, it must be logged in error.log file. e.g. I am using apache so my errors are logged in /var/log/apache2/error.log

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the response. Indeed, there are errors due to the fact that on the www-data user i do not have all python modules installed.
0

It's possible that the problem is the permission with the module, in this case pydub.

Comments

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.