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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。