Little help with sh shell please

Discussion in 'Site & Server Administration' started by JEET, Jun 8, 2010.

  1. #1
    Hi,
    I'm trying to restart bind using php: shell_exec("service named restart"), but for some reason, it keeps saying "stopping named...[failed] named already running"

    So I tried to make a sh file and executing it
    test.sh--------------
    #!/bin/sh
    service named restart
    ---------------

    Gives error:
    Usage: /etc/init.d/named {start|stop|status|restart|condrestart|reload|configtest|probe}
    : command not foundamed.sh: line 3:

    What am I missing here?
    Is there a different way to run this command in a sh file?
    Thanks :)
     
    JEET, Jun 8, 2010 IP
  2. raffo77

    raffo77 Active Member

    Messages:
    234
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #2
    copy it from webmin contrl panel :D
     
    raffo77, Jun 8, 2010 IP
  3. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #3
    Hi,
    I don't have webmin control panel. Could you post the command I need to run?
    Thanks :)
     
    JEET, Jun 8, 2010 IP
  4. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #4
    I also tried: (in php script)
    <?php echo shell_exec("/etc/init.d/named restart"); ?>
    Givse me this:
    Stopping named: ..................................................[FAILED]
    Starting named: named: already running[FAILED]

    When I use that command with putty, it works...
    What am I missing here?
    Thanks
     
    JEET, Jun 8, 2010 IP
  5. chtdatweb

    chtdatweb Well-Known Member

    Messages:
    1,473
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    160
    #5
    Have you just simply tried "service named stop" to see if the service will actually stop?
     
    chtdatweb, Jun 9, 2010 IP
  6. sysadmin

    sysadmin Peon

    Messages:
    111
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    or just try /etc/init.d/named restart . Let us know what is the result
     
    sysadmin, Jun 9, 2010 IP
  7. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #7
    Hi,
    Both these commands are working correctly in putty:
    service named restart
    /etc/init.d/named restart
    (Just "stop" and "start" work as well )
    However, when used inside a php script, they both return the same output:
    stopping named....[failed]
    starting named: named: already running [failed]

    The php script is:
    <?php echo shell_exec("/etc/init.d/named restart"); ?>
    or
    <?php echo shell_exec("service named restart"); ?>
    Both give same output as above.

    I also made a sh script to see if I can get that to work, but this is my first, and I can't get the sh script to work directly with putty, let alone with php.
    The sh script is as:
    #!/bin/bash
    F= "/etc/init.d/named stop"

    says: cannot find stop command

    I also tried:
    #!/bin/sh
    `service named restart`

    gives:
    Usage: service|stop|restart
    or something similar.


    I have spent the last 2 days doing nothing but searching the web with google, and still haven't found a sh script that would work, or a solution so I can directly get those commands working with php.
    I don't even know what I'm doing wrong with these.

    If I can get the service to restart automatically after 20-30 minutes anyway possible, even that would work. Like if there's a linux file I can edit and put the command in there.
    If you know a way to do this, please give me the command to paste in that file as well...

    Thanks :)
     
    JEET, Jun 9, 2010 IP
  8. RHS-Chris

    RHS-Chris Well-Known Member

    Messages:
    1,007
    Likes Received:
    35
    Best Answers:
    10
    Trophy Points:
    150
    #8
    Who is the owner of the files? Sounds like permission issues.
     
    RHS-Chris, Jun 9, 2010 IP
  9. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #9
    Aha! You were right. The sh script worked properly when I changed it's permissions to root:root
    But when I try to execute the script in shell_exec of php, it gives me a blank page for some reason.

    php code:
    <?php echo shell_exec("sh first.sh"); ?>

    I also changed the permission of this script like:
    chown root:root /home/site/test.php

    but still gives a blank page.
     
    JEET, Jun 9, 2010 IP
  10. sysadmin

    sysadmin Peon

    Messages:
    111
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    You can't execute this script via browser , Because your web server user won't we root ( apache or nobody )
     
    sysadmin, Jun 9, 2010 IP
  11. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #11
    oh. How do I restart this service using php then? On the shell_exec page of php.net I read someone using service restart command with php. How is this done?
    Thanks
     
    JEET, Jun 9, 2010 IP
  12. raffo77

    raffo77 Active Member

    Messages:
    234
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #12
    well,

    webmin use Perl and CGI script, if you want to use only PHP you must do this step:

    - create a new user
    - configure a vhost with suEXEC and assign php execution using the new user
    - give permission that allow your user to restart named service
    - be sure shell_exec are enable in php.ini
    - try, will be work

    So when you run the shell command you will run with a user that can access to the file/command. If you give full permission for nobody or www-apache.. you security will be compromise because also other vhost can run command.
     
    raffo77, Jun 10, 2010 IP
  13. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #13
    Hi,
    Thanks. Just 1 more thing. How can I do it with a CGI script then?
    I'm not very familiar with CGI, so if you could please post a code, or a link, that would be awesome.
    Thanks :)
     
    JEET, Jun 10, 2010 IP