use PHP to restart the linux machine

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shawnzhou888
Forum Newbie
Posts: 6
Joined: Wed Jun 04, 2003 2:29 pm

use PHP to restart the linux machine

Post by shawnzhou888 »

I tried to execute the following command to retart the linux machine but failed. Any idea? I tried other commands like exec, system, etc. All don't work.

<?php
shell_exec("shutdown r now");
?>
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

You need to have superuser permission to issue the shutdown command. PHP runs as the webserver (typically www-data or nobody) and has extremely restricted permissions.
shawnzhou888
Forum Newbie
Posts: 6
Joined: Wed Jun 04, 2003 2:29 pm

Post by shawnzhou888 »

Thanks for your quick answer, Eric. Does that mean the Apache web server must run as a root to enable the shutdown command?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Yes, but I would very strongly recommend against doing that.
shawnzhou888
Forum Newbie
Posts: 6
Joined: Wed Jun 04, 2003 2:29 pm

Post by shawnzhou888 »

I know it's dangrous. But what I am doing is an element management system. So the restart function is needed.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

take a look at http://www.courtesan.com/sudo/
and avoid apache running as root
Post Reply