I have an Ubuntu machine for headless selenium tests.

I generate a python script in /var/www/tmp/random123name.py via PHP and execute them. The script works when run from command line user, fails when run from web (apache www-data:www-data user).

The script recalls some system / python modules:

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from time import sleep, strftime
import os, json
from pyvirtualdisplay import Display

This code returns 1:

$python = "sudo /usr/bin/python /var/www/tmp/random123name.py";
exec($python, $output, $return);
echo "OUT<pre>".print_r($output,1)."</pre>"; #returns empty
echo "RET<pre>".print_r($return,1)."</pre>"; #returns 1

I modified /etc/sudoers this way, without luck:

www-data ALL=(ALL:ALL) NOPASSWD: /usr/bin/python

I also tried

www-data ALL=(ALL:ALL) NOPASSWD: /usr/bin/python /var/www/tmp/

added www-data to dialout group and some other tries. Thank you for your support.

  • Have you tried running the script from the command line as the www-data user? You can use su -s /bin/bash www-data to become the correct user. – Some Linux Nerd Dec 21 '15 at 23:10
  • What is in $python? Are you specifying the full path to your python interpreter and the full path the script? It might be that the command is not found. – Travis D Dec 22 '15 at 1:44
  • Thank you guys for the replies. I tried to run the script and it doesnt work. $python = "sudo /usr/bin/python /var/www/tmp/random123name.py" (I update my question) – fab Dec 22 '15 at 21:30
  • Have you tried calling /usr/bin/sudo rather than sudo? – Will Dec 26 '15 at 11:33

I solved: it's not enough to exit back to youruser / root, you need to exit ssh session and reopen it in order to load everything needed for visudo to work.


As you yourself have figured out, changes to sudoers don't get applied to running sessions, only to new sessions. So you need to logout and login again to see it.

But much more importantly, you shouldn't need sudo for this purpose, and allowing www-data user to run python as root without password is extremely scary. Surely there is a way to make your script work without needing root privileges, as a regular user, and I strongly urge you to try to figure that out, rather than resorting to sudo for this purpose.

  • luckily this VM is not exposed outside the LAN, but I'm willing to try any suggestion which would come, in order to have the lesson learned. – fab Dec 29 '15 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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