LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shutdown and Reboot Linux System via php script (https://www.linuxquestions.org/questions/linux-newbie-8/shutdown-and-reboot-linux-system-via-php-script-713379/)

ramasubbu1984 03-21-2009 12:38 AM

Shutdown and Reboot Linux System via php script
 
How to shutdown and reboot the linux system via php script. We have used more linux commands in php exec, shell_exec and system functions. But the commands are not working properly in php script. The commands are working in linux terminal. Please help me.

TB0ne 03-21-2009 03:39 PM

Quote:

Originally Posted by ramasubbu1984 (Post 3482797)
How to shutdown and reboot the linux system via php script. We have used more linux commands in php exec, shell_exec and system functions. But the commands are not working properly in php script. The commands are working in linux terminal. Please help me.

How about telling us what error(s) you're getting? What version/distro of Linux? Posting your script? You've got to tell us something more than "it's not working, please help".

ramasubbu1984 03-22-2009 11:06 PM

Shutdown and Reboot in linux system
 
How to shutdown and reboot the linux system via php script. We have used more linux commands in php exec, shell_exec and system functions. But the commands are not working properly in php script. The commands are working in linux terminal.

I m using Linux Fedora 7.0. I used more shell commands via php. But no errors display on the screen. Display a blank page only. But the display_errors option is enabled in my system and error_reporting also enabled(E_ALL).

I dont know the exact problem. But the shell commands are working in linux terminal. My system have full administrator permission.

php1.php
<?php
shell_exec("/usr/bin/reboot");
exec("/usr/bin/reboot");
system("/usr/bin/reboot");
?>

php2.php
<?php
shell_exec("/usr/sbin/reboot");
exec("/usr/sbin/reboot");
system("/usr/sbin/reboot");
?>

php3.php
<?php
shell_exec("shutdown");
exec("shutdown");
system("shutdown");
?>

php4.php
<?php
shell_exec("shutdown -r");
exec("shutdown -r");
system("shutdown -r");
?>

.....

?>

TB0ne 03-23-2009 11:38 AM

Quote:

Originally Posted by ramasubbu1984 (Post 3484575)
How to shutdown and reboot the linux system via php script. We have used more linux commands in php exec, shell_exec and system functions. But the commands are not working properly in php script. The commands are working in linux terminal.

I m using Linux Fedora 7.0. I used more shell commands via php. But no errors display on the screen. Display a blank page only. But the display_errors option is enabled in my system and error_reporting also enabled(E_ALL).

I dont know the exact problem. But the shell commands are working in linux terminal. My system have full administrator permission.

and more scripts.

The script you posted makes no sense at all. You're rebooting the system six times in a row, then shutting it down three times in a row, and then rebooting it again 3 times in a row. Isn't much of a script, just a list of system commands to be run. Also, you say "My system have full administrator permission". What does that mean? Do you mean your USER ID has administrative permission, since you can't give a WHOLE SYSTEM permissions....and you also say you don't know the exact problem.

Before anyone can give you any help, you're going to have to explain the problem. My initial guess is that you're running this 'script' as a root-equivalent user from the terminal, which has rights to run those commands. You don't say whether or not you're doing this from cron, via an AT job, or how, exactly you're running it.

Also, Fedora 7 is very old...Fedora 10 is the latest.

ramasubbu1984 03-24-2009 04:43 AM

Shutdown and Reboot in linux system
 
How to shutdown and reboot the linux system via php script. We have used more linux commands in php exec, shell_exec and system functions. But the commands are not working properly in php script. The commands are working in linux terminal.

I m using Linux Fedora 7.0. I used more shell commands via php. But no errors display on the screen. Display a blank page only. But the display_errors option is enabled in my system and error_reporting also enabled(E_ALL).

I dont know the exact problem. But the shell commands are working in linux terminal. My system have full administrator permission.

php1.php
<?php
shell_exec("/usr/bin/reboot");
exec("/usr/bin/reboot");
system("/usr/bin/reboot");
?>

php2.php
<?php
shell_exec("/usr/sbin/reboot");
exec("/usr/sbin/reboot");
system("/usr/sbin/reboot");
?>

php3.php
<?php
shell_exec("shutdown");
exec("shutdown");
system("shutdown");
?>

php4.php
<?php
shell_exec("shutdown -r");
exec("shutdown -r");
system("shutdown -r");
?>

.....

?>

and more scripts. Really thanks to TB0ne. Thanks for your reply. My system have all administrator privileges. My sytem have root privilege. Fedora 7.0 is support to execute linux commands like reboot ... commands in terminal. Boss all linux os with all versions are support to execute all types of linux commands in terminal. My system also support to execute linux commands like reboot commands are working fine. But the system oriented commands are not support to execute via php. The "ls" command is working fine via php. I want to develop one demo for handling system and apache, mysql service areas. For example shutdown, reboot, apache-start,stop,restart, mysql-start,stop, restart options. Do u have any reference script or site. Pls sent to me.

C/C++ files also used. But no response. This demo work is not based cron or AT or any other. Just for demo purpose only. I want to teach this commands and show a demo to my students.

grizly 03-24-2009 05:22 AM

Try using root

<?php
$command = "cat $pass | su -c 'shutdown -r now'";
$output = array();
try{
echo shell_exec($command);
exec($command, $output);
system($command, $output);
}Catch(Exception e){
print "Unable to shutdown system...\n";
}

foreach ($output as $line) {
print "$line<br>";
}
?>

This way you can store your root password in a config file, and your students can still understand the code.

Your students might understand something that wouldn't reboot the machine.. http://www.devguru.com/technologies/php/6010.asp

linuxlover.chaitanya 03-24-2009 05:27 AM

The shutdown and reboot command do not run from /usr/sbin but from /sbin/reboot.
You should need to change the complete path of the command from /usr/sbin/reboot to /sbin/reboot.
If you are not sure about it check with which command.

rweaver 03-24-2009 09:09 AM

Quote:

Originally Posted by ramasubbu1984 (Post 3482797)
How to shutdown and reboot the linux system via php script. We have used more linux commands in php exec, shell_exec and system functions. But the commands are not working properly in php script. The commands are working in linux terminal. Please help me.

You don't have the correct privileges. Web server scripts either execute as apache/httpd/nobody/whatever or the owning user (depending on setup) and typically if as user they can't be executed as any user <100.

A simple way to do it with out entering root passwords in php scripts would be to make a root crontab, take about a minute or two max to reboot the server.

root crontab
Code:

* * * * * /usr/local/sbin/checkreboot.sh
/usr/local/sbin/checkreboot.sh
Code:

#!/bin/bash
if [ -f /var/www/html/reboot.server ]; then
  rm -f /var/www/html/reboot.server
  /sbin/shutdown -r now
fi

reboot.php
Code:

<?php
fh = fopen("/var/www/html/reboot.server",'w');
fwrite($fh,"Reboot now\n");
fclose($fh);
?>

Of course, I don't suggest you actually deploy something like this unless you put a lot of authentication in it and error checking.

If you wanted it to be almost immediate you could make the script run in a loop if its not already running and testing for the file each iteration and sleeping for 1 at the end to prevent it from being to much of a drain on the system resources.

Also if memory serves me you could do the same thing via a snmp daemon and just issue an snmp call to the daemon to reboot the system.


All times are GMT -5. The time now is 11:19 AM.