On a web page I would like to have a button that launches a python script on a remote machine. Is there a way to do that in php? More specifically I would like this shell command to be executed on the remote machine:

screen -d -m ./script.py

I think it might be non-trivial because of permissions needed to execute scripts without root access.

2 Answers 2


First i will point out that this is unsecure approach in many levels. that being said:

If you have to be root to execute script.py then your website user has to have sudoers config. First you need to find out your website user.

Then set sudo privileges for that user so that he is able to execute script.py without password, i.e:

apache ALL=NOPASSWD: /full/path/to/your/script.py

Then add executable bit to your script.py:

chmod +x /full/path/to/your/script.py

Maybe you will have to change header of your py script to:

#! /usr/bin/python

After that in your php you can execute script with shell_exec for example:

$output = shell_exec('/full/path/to/your/script.py');

you could do that with following snipped but your request sounds like a security issue. It would be better if the remote host has a kind of API which you can query over HTTP

   $output = shell_exec('ssh user@remotehost script.py');
   echo "<pre>$output</pre>";

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .