how to do a linux reboot from php file

2019-01-26 14:15发布

I have a user brftv on my linux system and I have www-data that runs the nginx.

from the terminal I can let my brftv user run

sudo /sbin/reboot

and it works fine since I added the following to my /etc/sudoers file's "#user privilege specification" section:

brftv ALL=NOPASSWD: /sbin/halt, /sbin/reboot, /sbin/poweroff
www-data ALL=NOPASSWD: /sbin/halt, /sbin/reboot, /sbin/poweroff

But when my php file runs the following code, nothing happens

exec('nohup sudo -u brftv /sbin/reboot');

I added the www-data line to the etc/sudoers above in case it was necessary when running the above exec() (even though I run it as -u brftv, but I'm no linux expert, just thought better be safe just in case).

The php file that runs this exec() is owned by www-data, and chmod is 777, all should thus have privilege to execute from it.

I have tried running the php-file both through browser (would be run by user www-data I assume) and from terminal $ php myFile.php.

------------------- UPDATE -----------------

I did this

sudo chmod u s /sbin/reboot

Which allows all users on my system to run the reboot cmd without password. It works, but I rather not leave it THAT open, so the other solution with /etc/sudoers would be better, if someone would have a hint at what my problem is...

I followed this tut http://linux.byexamples.com/archives/315/how-to-shutdown-and-reboot-without-sudo-password/ and the second example is pretty much what I got above that didn't work for me..

7条回答
2楼-- · 2019-01-26 15:18

try this code exec('sudo shutdown -r now');

or "shutdown -r +5" shutdown after 5 minutes

查看更多
登录 后发表回答