element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi Projects
  • Products
  • Raspberry Pi
  • Raspberry Pi Projects
  • More
  • Cancel
Raspberry Pi Projects
Blog Pi Webpage Reboot
  • Blog
  • Documents
  • Events
  • Polls
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi Projects to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: PEarle
  • Date Created: 2 Apr 2014 9:01 AM Date Created
  • Views 4486 views
  • Likes 2 likes
  • Comments 8 comments
  • raspberry_pi_projects
Related
Recommended

Pi Webpage Reboot

PEarle
PEarle
2 Apr 2014

I have recently set up a LAMP Raspberry Pi and am using this at work to monitor some servers (see Raspberry PI as a LAMP server and PI as a Web Server Monitor)

I'm running it 'headless' (i.e. without a keyboard or mouse attached). This is fine until I wanted to reboot it yesterday, and realised that I would have to go through the hassle of opening a Putty session, connecting to the server, logging on, and issuing the reboot command. How convenient it would be if i could just click on a web link ...

 

After some investigation I discovered that I couldn't simply write a .php page with something like 'sudo reboot' in it. What I needed to do was ;

 

  • Write a python script that would reboot the server
  • Write a php page to execute the python script.
  • I would also need to make some configuration changes to allow the reboot command to be run from a web application

 

Python Script

If I was booting from the command line I would enter sudo reboot. My python script therefore needs to allow me to run a system command. This is done by using the python subprocess command (there's a useful reference page at Subprocess and Shell Commands in Python ). With that knowledge, the resulting code is fairly easy to understand - see below. Note that the 'sudo' and  'reboot' commands are evaluated with their full paths.

 

command = "/usr/bin/sudo /sbin/reboot"
import subprocess
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
output = process.communicate()[0]
print output
   

 

Time stamping

 

I decided that it would be nice to keep a record of the commands that were being issued as added a time stamp operation in the python code - this simply writes a line to a log file with the current date/time. I also found this useful when developing this mini app as I could have the script run (and see that it had run successfully!) without having to issue the 'reboot' command.

 

The timestamp code is as follows

 

#
# Construct a timestamp variable
#
import time
ts = time.time()
import datetime
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')


#
# Open a file and allow content to be appended to it
#
f = open('/var/www/remotecontrol/logs/reboot.log', 'a')


# write the timestamp and text to the file
f.write(st)
f.write(' : REBOOT command issued\n')
  

 

For the entire script, see attached example.

 

PHP Page

 

Once I had the script, I needed a page to allow me to run it. Executing the script from a web page is quite straight forward - e.g.

 

<?php
 exec("python /var/www/remotecontrol/python/reboot.py");
?>
  


However I want to control the process so that the script only runs when a button is pressed. Therefore my page needs to :


  • Display a page with a button on it
  • When the user presses the button
    • Execute the script
    • Display a confirmation message


I achieved this with the following code :


Output a button - when it is clicked execute javascript function reboot().


<input type="button" id="reboot"  value="Reboot" onClick="reboot()"/>


The javascript function resubmits the page with an added parameter - i.e. status=reboot


<script>
  function reboot() {
       window.location="reboot.php?status=reboot";
  }
</script>
  

 


Php code in the page looks for the existence of the 'status' parameter and acts accordingly - note that it executes the script and puts out an indicationon the screen that the server is rebooting


<?php
   $status = $_GET["status"];

  if ($status == "reboot"){
       exec("python /var/www/remotecontrol/python/reboot.py");
       print '<h2>Server is rebooting ...</h2>';
  }
?>
  


The full code is attached below - note that the page offers two options reboot and shutdown. (The shutdown script is a clone of the reboot script - with the command changed to shutdown now.)

 

Configuration Changes

When I tried to run the function nothing happened - my page displayed the 'Server is rebooting' but the server didn't reboot. I looked in the logs and found sudo: no tty present and no askpass program specified. (NB the error.log file is located at /var/log/apache2). After some 'googling' I discovered that this was being issued because the current user doesn't have access to the command. although I always log on to the pi from the command line as the default user pi, this command is being issued by the web page via Apache - and with the default LAMP installation, Apache runs as user www.data, therefore, this user (i.e. Apache) needs to be given access to the sudo command to run the reboot command.


This is achieved as follows.

  • From a putty session use command sudo visudo
    • This will open up the sudo configuration file in the editor.
  • Go to the end of the file and add the following line


www-data ALL=/sbin/reboot
www-data ALL=NOPASSWD: /sbin/reboot
 

the effect of tis will be to enable user www-data to use the reboot command. if you want to issue other commands using sudo from Apache (e.g. shutdown, then add additional lines to the sudo configuration file.

 

When I had made the changes I rebooted the Pi to make sure the changes were applied.

 

 

That's it! - when I go to the url http://10.13.36.255/remotecontrol/reboot.php I see the following page; (I may add some styling to make it look better !)

 

image

 

 

When I click on the 'reboot button I see this, and the Pi reboots !

 

image

If this was anywhere other than in a very closed environment I would add some extra functionality to prevent the reboot page being actioned by unwanted users ! - but in this particular case I don't need to worry image

 

Useful links

 

Raspberry Pi as a LAMP Serverhttp://www.element14.com/community/community/raspberry-pi/raspberrypi_projects/blog/2014/02/24/raspberry-pi-as-a-lamp-server
Pi as a Webserver Monitorhttp://www.element14.com/community/community/raspberry-pi/raspberrypi_projects/blog/2014/03/20/pi-as-a-web-server-monitor
Python Subprocess Commandhttp://www.pythonforbeginners.com/os/subprocess-for-system-administrators

 

 


 








Attachments:
rebootaction.zip
rebootprompt.zip
reboot.zip
  • Sign in to reply
  • Former Member
    Former Member over 9 years ago in reply to PEarle

    Many thanks, I'll have a crash about with that, much appreciated! Always good to learn.

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • PEarle
    PEarle over 9 years ago

    I'm afraid I haven't got access to the original code any more - however, the main function is to be able to issue the reboot command from a web page - the rest of the article just deals with some bells and whistles to make it look nicer, etc. The functionality to allow a php page to issue the reboot command is dealt with in the first code example on this blog post (i.e. under the heading Python Script). If you look at that, followed by the section PHP Code, it should be enough to explain how to get the function up and running.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Former Member
    Former Member over 9 years ago in reply to shabaz

    I successfully downloaded and unzipped the files using Chrome and have opened and tried them. As a previous contributor mentioned I'm not sure if some files or parts of the code are missing, e.g., should there be a reboot.php?

     

    I plan to use my Pi3 with a picamera in headless mode to take timelapse sequences. The Pi is set up as a local access point with a web interface with buttons so that I can set different timelapse commands running or take still shots or video via the Pi's onboard wifi using my android phone or tablet, I have got that working. But I have been looking for a way to safely cancel whatever the Pi is doing through the web interface, avoiding having to just pull the plug, using sudo reboot or sudo shutdown or similar. When I found this page I thought it seemed ideal for what I needed.

     

    Any suggestions please.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • shabaz
    shabaz over 9 years ago in reply to Former Member

    The file attachments (reboot.zip, rebootprompt.zip and rebootaction.zip download fine for me, I just tried. They unzip fine too.

    I used Chrome. Could you try Chrome, or a different browser or PC?

     

    You didn't specify details about what software you are using for your project but unless you're already using LAMP as the blog author was for other purposes, it is severe overkill to install LAMP

    just to be able to reboot remotely.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Former Member
    Former Member over 9 years ago

    This is exactly what I would like to do to cancel or reboot to stop a headless timelapse running. But as a previous contributor has said the downloads do not appear to be complete. Please can someone help?

     

    Thanks

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube