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:
<?php
$result = shell_exec('sh /home/user/Desktop/script.sh');
echo $result; (just to make sure it fired)
?>
Shell script:
#!/bin/bash
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.
sudo
that supposed to ask a password for elevated privilege. Either you need to disable passwords onsudo
(which is a bad idea) and add web user (www, or www-data or one who runsphp
from web) to the group ofsudo
or rethink your task, it pretty dangerous to giving access from web to the root's privileges – Alex Feb 8 '17 at 0:18sudo
- apart from being dangerous, it's likely (depending on configuration) to drop environment variables such asDISPLAY
andXAUTHORITY
that will be needed by the X11 application. – Toby Speight Feb 8 '17 at 14:42