Find the answer to your Linux question:
Results 1 to 6 of 6
Hello, I've been at this for awhile now. I wrote a shellscript, that when executed from the commandline works fine and exits fine (giving me commandline back) when its done ...
  1. #1
    Just Joined!
    Join Date
    May 2005
    Location
    Canada
    Posts
    3

    PHP webpage HANGS: executing shellscript.sh via system()

    Hello,

    I've been at this for awhile now. I wrote a shellscript, that when executed from the commandline works fine and exits fine (giving me commandline back) when its done (takes 2 seconds)

    I have now written a PHP page, that when visited by the user will execute the shellscript in the user's personal files. (they dont have ssh access, hence the use of php).

    This PHP page in question uses the system() command to execute the shellscript (and the shellscript actually does get executed). The problem is, even though running the shellscript was a success, PHP will hang and not load the page.

    I am assuming it is because PHP wont let go of the file..I have tried many variations..here are some examples:

    $last_line = system("nohup ".$target."/start.sh > /dev/null 2>&1 &");

    $last_line = system($target."/start.sh &");


    I have even tried substituting system() with exec(). The shell script does echo about 4 lines of output. I would very much so like to return this output to the PHP page as well, please

    What am I doing wrong or what should I be doing instead? Also after php hangs, I get this in ps aux:

    testuser 24229 0.0 0.0 0 0 ? Z 02:27 0:00 [sh <defunct>]


    the 'sh' would be due to the shellscript. I am 100% sure the shellscript is okay. If I try to run an executable directly (instead of start.sh) using system(), then the executable filename will be the defunct instead of sh.

    I would be very grateful for your help on this. I have read php.net manuals and tried many things.. I have exhausted my resources :\

    it might be interesting to note that this one doesnt hang:

    $last_line = exec($target."/mainfile stop &"); // stops the program


    ./mainfile stop would close the program in question. I guess it works because it closes so PHP lets go of it. If you think it would help to view the start.sh file let me know and I will post it. I am using these:

    #!/usr/local/bin/php (correct path)
    #!/bin/sh (correct path)

    Thank you kindly, means alot..

    -Ji

  2. #2
    Linux Engineer Giro's Avatar
    Join Date
    Jul 2003
    Location
    England
    Posts
    1,219
    This is a zombie proccess,

    Code:
    testuser 24229 0.0 0.0 0 0 ? Z 02&#58;27 0&#58;00 &#91;sh <defunct>&#93;
    Im not sure whats causeing the problem but have you tried moving the commands inside the shell script directly into the system() call, also have you put a debug message before the call and after?

  3. #3
    Linux Guru kkubasik's Avatar
    Join Date
    Mar 2004
    Location
    Lat: 39:03:51N Lon: 77:14:37W
    Posts
    2,396
    other possible might be that the shell script doesn't return what the php system() call expects to indicate that the call has been finished. You'll have to check the docs, but its possible that a TERMINATE_SUCESS of somesort is required before php will finish parsing.

    Also try making a file

    #!/usr/bin/php

    <php?
    system(shellscript)
    php?>


    and running it, its possible that its just your mod_php isnt working, or that apache doesnt have permisson to run the shell script correctly (and if the shell script doesnt have a terminationg built in, it might hang on a permissions issues that it doesnt have when you test it under your normal account)
    Avoid the Gates of Hell. Use Linux
    A Penny for your Thoughts

    Formerly Known as qub333

  4. #4
    Just Joined!
    Join Date
    May 2005
    Location
    Canada
    Posts
    3
    Thank you for your responses,

    I have not yet attempted to have PHP perform all the functions of the shellscript...however one of the lines inside the Shellscript is an executution of the main 'soldatserver' anyways, and produces the same result (php hanging) as it does trying to run the shellscript would.

    (I have tried having system(ls -l); and it outputted the ls command just fine and did not hang)

    When I do debugging, I use:

    error_reporting(E_ALL); ... But in this case the php webpage does not even load at all so I can't see any of the debugging output.

    When the php webpage is told to start the shellscript, it does actually run the shellscript fine. Because the 'soldatguardian' the shellscript runs, does start correctly.

    I am kind of thinking it is because PHP keeps a handle on the program it starts and won't let go or something..

    Here is the shellscript just in case:
    Code:
    #!/bin/sh
    
    curdir="/home/s4r/servers/s4r2"
    
    echo "Checking Server Environment..."
    process=`ps uxw | grep $curdir/soldatserver | grep -v grep | awk '&#123;print $11&#125;'`
    if &#91; -z "$process" &#93;; then
    echo "Server is not active."
    echo "Initializing Server Process..."
    $curdir/soldatguardian
    sleep 1
    echo "Server Started Successfully"
    else
    echo "ERROR&#58; Server Already Active"
    fi
    
    exit 1
    It simply checks to see if the soldatserver is running, and if it isnt then it spawns it by running soldatguardian (which is a crash-prevention addon that handles launching the soldatserver instead). All of these run fine from commandline.

    start.sh - runs fine from commandline, finishes in 2 seconds gives commandline back
    soldatserver - runs fine from commandline, but uses commandline for all its server output
    soldatguardian - runs fine from commandline, spawns soldatserver but moves it's output to a log file. gives commandline back

    This code works fine to have PHP stop the server and load the page:

    $last_line = exec($target."/soldatguardian stop &");

    I am stumped..It is strange that stopping it doesnt cause hang but starting it does :\

    Thank you for your help I am grateful

    -Ji

  5. #5
    Just Joined!
    Join Date
    May 2005
    Location
    Canada
    Posts
    3
    Quote Originally Posted by qub333
    Also try making a file

    #!/usr/bin/php

    <php?
    system(shellscript)
    php?>

    and running it, its possible that its just your mod_php isnt working
    Is it just plain old system(shellscript) or system(/path/to/a/shellscript.sh) ?
    I guess I should try both. What should this do?

    And what may I ask is mod_php ? The system($target."/server stop &"); command worked...would that be an indication that I can do what I need? Except that command is to stop the server.. I need it to work when I am trying to start it

    When I do try to start, I know the shellscript runs and executes fine. But php hangs. I should probably try to do that, and while php is hanging, stop the server via commandline, and then see if php will suddenly load :P

    -Ji

  6. #6
    Just Joined!
    Join Date
    Jun 2006
    Posts
    1
    I had the same problem as you trying to get my mp3_randomzier to execute a the command to play a new song after n seconds. This code seems to work .

    Code:
            system("nohup thecommand > /dev/null & echo $!;");
    - dark2k1

Posting Permissions

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