PHP To Shell (Shell_Exec)

Discussion in 'PHP' started by CarbonLife, Jan 24, 2010.

  1. #1
    I have a PHP script which validates two variables, then passes them to a shell script. The script does work fine but when php runs shell_exec it waits until the shell script has finished running. The script script could take several minutes in which time PHP will timeout. How can I pass the two variables on without php waiting for the script to finish?

    Example...
    
    <?php
    shell_exec('/var/shell.sh $1 $2');
    ?>
    
    Code (markup):
     
    CarbonLife, Jan 24, 2010 IP
  2. hasanbasri

    hasanbasri Peon

    Messages:
    78
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    set_time_limit(5);// you may change that
    $output=@shell_exec('/var/shell.sh $1 $2');
    set_time_limit(30);// default
    if($ouutput=NULL)
    echo "not executed";
    else
    echo "executed";
    
    Code (markup):
    is that work?
     
    hasanbasri, Jan 24, 2010 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    Try something like this:

    
    
    shell_exec("/var/shell.sh $1 $2 > /dev/null &");
    
    
    PHP:
    This should work with exec, system, shell_exec, passthrough, etc...
     
    jestep, Jan 25, 2010 IP