Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2012
    Thanked 0 Times in 0 Posts

    Executing shell script from web sever via URL/button?

    Hey guys,

    I am trying to execute a shell script to shutdown a FreeNAS serve from my web server. I want to be able to click a button or just type in a url to execute the script from my raspberry pi that will shutdown a different computer (freenas).

    Currently, I have setup ssh keys and I can connect from my raspberry pi to freenas fine through ssh.

    This is my command that successfully turns off my freenas when executing it from terminal/putty whilst already sshed into my raspberry pi:

    pi@raspberrypi ~ $ sudo ssh root@ -i /etc/ssh/freenas-root_rsa 'shutdown -p now'
    Shutdown NOW!
    shutdown: [pid 16089]
    Shutdown NOW!
    ^^^^ this works.

    Now onto the next step which I am having trouble with.

    I created a .sh script e.g. shutdown.sh and put this into /usr/lib/cgi-bin directory. Script is as below. Permissions set to 755 so it's executable.

    sudo ssh root@ -i /etc/ssh/freenas-root_rsa 'shutdown -p now'
    Now when I try to execute it by url:

    nothing happens to my frenas server.

    I do get a server 500 error but i assume this is because there is no html for the page to load.

    How can I get this script to work when executing it from a html <a>, or <button> button or just from entering a URL as above.


  2. #2
    Master Coder
    Join Date
    Feb 2011
    Your Monitor
    Thanked 617 Times in 603 Posts
    I think you have to use the exec() function instead to run the script. You can't run .sh remotely via url.

  3. #3
    New Coder
    Join Date
    Nov 2012
    Thanked 0 Times in 0 Posts
    hey tangoforce,

    I have also tried that.

    I have a php page e.g. index.php that does this:
    if ($_GET['run']) {
    # This code will run if ?run=true is set.

    <!doctype html>
    <meta charset="UTF-8">
    <title>Untitled Document</title>


    <!-- This link will add ?run=true to your URL, myfilename.php?run=true -->
    <a href="?run=true">FreeNAS Shutdown</a>

    but this still does not shutdown my freenas.

  4. #4
    Senior Coder
    Join Date
    Sep 2010
    Thanked 275 Times in 275 Posts
    On a Linux machine a server is a 'service' i.e. a daemon. For instance to start and stop Apache, which is designated as httpd, the d is for daemon, you just use

    service httpd start

    or service httpd stop

    Using backslash notation on a php page it would be

    `service httpd stop`

    I'm quite sure that if you find the right designation for freenas you could just make the substitution.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.

    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts