Shutdown Debian from php script

0

I am trying to shutdown my RaspberryPi running Raspbian from a php script running on lighttpd webserver. shell_exec("sudo /sbin/shutdown -h now 2>&1");

I added lighttpd ALL=(ALL) NOPASSWD: /sbin/shutdown to my /etc/sudoers file and checked that Defaults !requiretty is set.

I still get "sudo: no tty present and no askpass program specified" when running the script.

Can somebody please help me? Thanks!

frsc

Posted 2013-04-25T19:27:59.287

Reputation: 103

I think you may want the shell_exec function instead of exec since that is a shell command (I think that's how it works). I am not 100% sure (hence the comment), but may be worth a try. – nerdwaller – 2013-04-25T19:33:15.920

Output redirection (2>&1) requires a shell; try removing that from the command and see if you get a different result. – Aaron Miller – 2013-04-25T20:14:15.197

Thanks for your comments. Actually I already tried shell_exec in the first place. I just have copied the wrong command to my question. And exec("sudo /sbin/shutdown -h now"); doesn't work either. – frsc – 2013-04-25T20:34:54.293

Are you sure that lighttpd is running as user lighttpd? In a php script, echo \whoami``; It might be www-data or http instead – WJDev – 2013-04-25T20:57:44.927

Answers

1

Lighttpd might not be running as user lighttpd.

In a PHP script, write the following to find out what user it runs with:

echo shell_exec('whoami');

It might be www-data or http instead.

WJDev

Posted 2013-04-25T19:27:59.287

Reputation: 26

Thank you ElanMan for your hint. The correct user was indeed www-data and not lighttpd as I have read somwehere else. Now it works as expected. – frsc – 2013-04-26T13:03:28.573