LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 03-21-2009, 01:38 AM   #1
ramasubbu1984
LQ Newbie
 
Registered: Mar 2009
Posts: 3

Rep: Reputation: 0
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.
 
Old 03-21-2009, 04:39 PM   #2
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 15,078

Rep: Reputation: 2713Reputation: 2713Reputation: 2713Reputation: 2713Reputation: 2713Reputation: 2713Reputation: 2713Reputation: 2713Reputation: 2713Reputation: 2713Reputation: 2713
Quote:
Originally Posted by ramasubbu1984 View Post
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".
 
Old 03-23-2009, 12:06 AM   #3
ramasubbu1984
LQ Newbie
 
Registered: Mar 2009
Posts: 3

Original Poster
Rep: Reputation: 0
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");
?>

.....

?>

Last edited by ramasubbu1984; 03-24-2009 at 05:44 AM.
 
Old 03-23-2009, 12:38 PM   #4
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 15,078

Rep: Reputation: 2713Reputation: 2713Reputation: 2713Reputation: 2713Reputation: 2713Reputation: 2713Reputation: 2713Reputation: 2713Reputation: 2713Reputation: 2713Reputation: 2713
Quote:
Originally Posted by ramasubbu1984 View Post
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.
 
Old 03-24-2009, 05:43 AM   #5
ramasubbu1984
LQ Newbie
 
Registered: Mar 2009
Posts: 3

Original Poster
Rep: Reputation: 0
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.
 
Old 03-24-2009, 06:22 AM   #6
grizly
Member
 
Registered: Nov 2006
Location: Melbourne Australia
Distribution: Centos, RHEL, Debian, Ubuntu, Mint
Posts: 128

Rep: Reputation: 16
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

Last edited by grizly; 03-24-2009 at 06:29 AM.
 
Old 03-24-2009, 06:27 AM   #7
linuxlover.chaitanya
Senior Member
 
Registered: Apr 2008
Location: Nagpur, India
Distribution: Cent OS 5/6, Ubuntu Server 10.04
Posts: 4,628

Rep: Reputation: Disabled
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.
 
Old 03-24-2009, 10:09 AM   #8
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 163Reputation: 163
Quote:
Originally Posted by ramasubbu1984 View Post
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.

Last edited by rweaver; 03-24-2009 at 10:20 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run script on shutdown or reboot VeeDubbs Linux - Server 1 10-23-2008 02:34 PM
how to change the system date during shutdown or reboot. ph2932 Linux - General 2 05-20-2007 03:19 PM
reboot or shutdown system stu_zamani Programming 3 02-10-2007 03:29 PM
Shutdown the System remotely via PHP-Script? linuxuser2005 Linux - General 2 08-30-2004 12:53 PM
System Shutdown/Reboot freezes ?? Zander Linux - General 1 12-07-2002 11:27 PM


All times are GMT -5. The time now is 12:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration