简体   繁体   中英

Starting a bash script from a php script

I'm trying to run a bash script using shell_exec but It doesnt seem to work. (Nothing seems to happen) I'm using nginx and the latest php5-cgi. Heres what the php file looks like:

<?php

$startserver = "./startserver.sh";
$startserver = shell_exec($startserver);
$getprocess = "pidof hlds_amd";
$pid = shell_exec($getprocess);

$fh = fopen('closeserver.sh', 'w');
$command = "kill -9 $pid";
fwrite($fh, $command);
fclose($fh);


$string = "at -f closeserver.sh now + 1 hour";
$closer = shell_exec($string);  


?>

and this is what the bash script looks like:

#!/bin/bash
cd /home/kraffs/srcds
./hlds_run -game cstrike -autoupdate +maxplayers 12 +map de_dust2 > hlds.log 2>&1 &

Theres no errors in the phpscript and the file gets created just fine but $startserver doesnt seem to get executed and $pid is empty. Did I miss something in the php file or do I need to change permissions for an user? Thanks for your help.

replace shell_exec, with below function and try again

<?php
function runcmd($EXEC_CMD)

    $handle = popen ($EXEC_CMD, 'r');
    $output = "";
    if ($handle) {
        while(! feof ($handle)) {
            $read = fgets ($handle);
            $output .= $read;
        } 
        pclose($handle);
    }
    return $output;
}  
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM