drghhp8706 2017-11-05 15:06
浏览 43
已采纳

shell脚本在控制台上工作,但不是从php页面

I have a problem, I need to launch a .sh script from a web page, just does not go, starting from the terminal the script works and does what it has to do, but from the web no, but in the ' Apache2 error.log does not make any mistakes, I do not understand what it can be ..

HTML:

    <tr>
        <td>JTS</td>
        <td>
            <form action="JTSstart.php">
            <input type="submit" value="START">
            </form>
        </td>
                <td>
            <form action="JTSres.php">
            <input type="submit" value="RESTART">
            </form>
        </td>
                <td>
            <form action="JTSstop.php">
            <input type="submit" value="STOP">
            </form>
        </td>
    </tr>

PHP:

<?php
echo exec('bash JTSstop.sh');
sleep(5);
header("Location: 5ondimba.html");
?>

SH:

#!/bin/bash
cd  /home/otaku/JTS3ServerMod_HostingEdition
./jts3servermod_startscript.sh stop

What I have tried / tested: 1) The exec command, such as shell_exec, is not disabled in the php setup. 2) The files were converted with dos2unix. 3) bash -x on the script and does not report any kind of error (in fact, from console works).

what could it be?? how can i make it work? Thanks so much!

</div>
  • 写回答

1条回答 默认 最新

  • dphphvs496524 2017-11-05 15:28
    关注

    I see a few problems:

    1) Get rid of echo. At best, it will display output of the command (if there is any), but if that happens it will cause your subsequent header() to fail - header() must be called before any output is generated.

    2) PATHs are not the same in PHP as in your SHELL on the console. Don't assume bash or JTSstop.sh are in PHP's PATH, or in PHP's current working directory. Better to always fully specify paths.

    3) Your script already includes #!/bin/bash, no need to call it with bash again.

    PHP:

    <?php
    exec('/full/path/to/JTSstop.sh');
    sleep(5);
    header("Location: 5ondimba.html");
    ?>
    

    But why not keep things simple and get rid of JTSstop.sh all together?

    <?php
    exec('cd /home/otaku/JTS3ServerMod_HostingEdition; ./jts3servermod_startscript.sh stop');
    sleep(5);
    header("Location: 5ondimba.html");
    ?>
    

    If you're still having problems, you can see the results of the exec by specifying a 2nd parameter, as described in the docs.

    <?php
    exec('cd /home/otaku/JTS3ServerMod_HostingEdition; ./jts3servermod_startscript.sh stop', $output);
    print_r($output);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动