LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   php halt script (https://www.linuxquestions.org/questions/programming-9/php-halt-script-469353/)

spank 07-31-2006 07:31 AM

php halt script
 
I want to do a php script that halts the pc.
how can i do that?
i've tried:
Code:

<?
exec('halt');
?>

but it doesn't happen anything.
how can i solve it?

thank you!

vharishankar 07-31-2006 07:46 AM

Maybe there's a good reason not to call this function from PHP. The permissions for halt on many machines might rest only with the super user. PHP usually doesn't (and shouldn't) run as super user on the server.

spank 07-31-2006 07:51 AM

yes i'm aware of the risk that doing this think implies but i still want to do it. any ideeas on how to do it ?

musicman_ace 07-31-2006 07:54 AM

Shouldn't it be
<?
system('halt')
?>


Also, whomever the apache server is running as, give that user sudo priveledge for halt and then try your code running sudo halt. I'm not sure if it'll work, but its a thought.

spank 07-31-2006 07:56 AM

it's the same thing

vharishankar 07-31-2006 08:10 AM

Without knowing your system configuration we're only going to guess why it doesn't work. You haven't even given any error messages (if any) or how PHP is set up in your system. We don't even know whether you're calling this from the command line... I'm assuming you are.

spank 07-31-2006 11:36 AM

ok.... sorry about that.

I have Apache/2.0.55 and PHP/5.1.2 on an Ubuntu Server 6.06. I just want to create a php script with which i want to shutdown the server via my browser.
i've created test.php
Code:

<?
        echo "<hr>";
        echo system('halt');
        echo "<br><hr>";
?>

between the lines i don't get any error message.

NOTE: as a desperate method i've chmoded /sbin/halt to 777 and it still doesn't work.

taylor_venable 07-31-2006 04:27 PM

Do you have safe mode enabled? Also, what do your Apache error logs say?

spank 08-02-2006 12:56 PM

in error.log i have:
Code:

halt: must be superuser.
how can i make user www-data be able to halt the computer ?

unSpawn 08-02-2006 02:39 PM

Like musicman_ace already said you better check out sudo.

Mcribs 08-02-2006 02:59 PM

I thought system commands were called in the scipt through the usage of "print `command`;"?

spank 08-02-2006 03:06 PM

how can i do the sudo thing?

i do a visudo and add the lines:
Code:

Cmnd_Alias      HALT = /sbin/halt
www-data        ALL= HALT

and modify the script to:
Code:

<?
        echo "<hr>";
        echo system('sudo /sbin/halt');
        echo "<br><hr>";
?>

but if i check the error.log it says:
Code:

Passwd:
i guess it prompts me for a passwd. shouldn't sudo halt work without asking for the pass? how can i enter the pass?

taylor_venable 08-02-2006 07:45 PM

Sudo by default requires a password (for the obvious security reasons). If you're absolutely positive that you want to do this, check out the documentation: http://www.courtesan.com/sudo/man/su...swd_and_passwd.

spank 08-03-2006 02:58 AM

:) thank you... solved that. I added the line to /etc/sudoers:
Code:

www-data        ALL= NOPASSWD: /sbin/halt
and then all went smothly.


All times are GMT -5. The time now is 01:34 PM.