I am fairly new to php, but I was trying to use php to launch a shell script.

When I use php /var/www/html/indexes.php it works, but when I load up the webpage the shell script does not do all of what it is meant to.

PHP file:

$result = shell_exec('sh /home/user/Desktop/script.sh');
echo $result; (just to make sure it fired)

Shell script:

echo "Hello World"
sudo -u user nautilus 

When I fire the php through the console it works, but when I load the webpage up it does not. I have run chmod +x on both the php and shell scripts, with no luck. It returns the Hello World, but does not fire up the nautilus as the user. I don't know but maybe it has something to do with the X server? Any help would be greatly appreciated.

  • You using in your shell script command sudo that supposed to ask a password for elevated privilege. Either you need to disable passwords on sudo (which is a bad idea) and add web user (www, or www-data or one who runs php from web) to the group of sudo or rethink your task, it pretty dangerous to giving access from web to the root's privileges – Alex Feb 8 '17 at 0:18
  • have you check error.log/access.log ? – Archemar Feb 8 '17 at 6:39
  • Don't use sudo - apart from being dangerous, it's likely (depending on configuration) to drop environment variables such as DISPLAY and XAUTHORITY that will be needed by the X11 application. – Toby Speight Feb 8 '17 at 14:42

For using the system commands on webpages through php you have to provide full path to the command from where it stored.

For example

>/usr/bin/nautilius [parameter]

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.