Python script via PHP

Asked

Viewed 738 times

1

I’m trying to call a script done in Python via browser with PHP. This script has only one command that serves to turn off my Raspberry Pi.

PHP is that way:

<?php
 exec('sudo python /home/pi/Documents/Programa/Script.py');
?>

Python script:

import os
os.system('sudo shutdown -r now')

I ran the program in Python and PHP (via terminal) separately and the two are working properly. Only when I call in the browser that it does not run. I know you have to change folder and file permissions, but I don’t know exactly which directories I have to give these permissions because I’m a beginner in the world of PHP and linux. I have already searched on some other site ways to do this.

  • 1

    Why don’t you run "shutdown" straight from PHP? This script doesn’t even have any Python.

  • Hello jsbueno, thank you for your reply. Do you tell me to put the "sudo shutdown -r now" command straight into php exec()? That I’ve done, it doesn’t work either.

  • Shows no error message? ever seen in php or server log?

  • Hi Julio Neto, thanks for the reply. I do not know where the log, I will look to see if any message appears there and I return with the reply.

1 answer

2


php is not allowed to turn off the computer. It runs with an internal service user, (usually www-data), and this user is not allowed to run the sudo.

When you run with your user, on the terminal, it works because your user is allowed to use the sudo, but when making php run the command, it will fail, because the user running the php server does not have this permission.

  • Hello, I understood your answer, but in case there is no way I give this permission for PHP? I saw in one of the links I mentioned above that you can give permissions to certain files and folders, but since I don’t know much about linux(Raspbian) I don’t know exactly how to do.

  • 1

    @Q.Wesley As the nosklo spoke, it is only a matter of permission before the sudo. If you add the PHP user, commonly www-data (confirm), to the list of sudoers, who are users with permission to sudo, you will probably be able to do something like this. But first of all, I recommend reviewing the jsbueno, because I have no reason to do this with Python and you can run the command from PHP.

  • @Q.Wesley, yes, Anderson is right. You can run shutdown directly from php, you don’t need python intermediate. The question is to set up your sudo - you can ask a question about how to set up the sudo in superuser.com (English)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.