Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!

share|improve this question
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 Apr 25 '13 at 19:33
Output redirection (2>&1) requires a shell; try removing that from the command and see if you get a different result. – Aaron Miller Apr 25 '13 at 20:14
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 Apr 25 '13 at 20:34
Are you sure that lighttpd is running as user lighttpd? In a php script, echo `whoami`; It might be www-data or http instead – ElanMan Apr 25 '13 at 20:57
up vote 1 down vote accepted

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.

share|improve this answer
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 Apr 26 '13 at 13:03

You must log in to answer this question.

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