0

我正在尝试使用 shell_exec 运行 bash 脚本,但它似乎不起作用。(似乎什么也没发生)我正在使用 nginx 和最新的 php5-cgi。下面是 php 文件的样子:

<?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);  


?>

这就是 bash 脚本的样子:

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

phpscript 中没有错误,文件创建得很好,但 $startserver 似乎没有被执行并且 $pid 为空。我错过了 php 文件中的某些内容还是需要更改用户的权限?谢谢你的帮助。

4

1 回答 1

1

用以下函数替换 shell_exec,然后重试

<?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;
}  
?>
于 2011-03-29T16:00:59.180 回答