在我的本地服务器上用php运行一个bash脚本


Run a bash script with php that is on my local server

我知道我可以使用php运行bash命令,但它超时了,我需要它连续运行。我希望这个网站调用服务器上的bash文件而不是在php

中运行

在php脚本的开头使用以下代码:

<?php
ini_set('max_execution_time', 0);
...

其中0表示永远运行/直到脚本结束。或者设定合理的秒限

这是我用来通过php运行bash脚本的:

<?php
    set_time_limit(0); // run for unlimited time
    ignore_user_abort(1); //runs even if the user closes the browser/shell session
    $command = $_GET['c'];
    $pass =  $_GET['p'];
    if($p == "my password" & !EMPTY($command)){

        switch ($command){
            case "dothis":
                $command = "sh /path/to/dothis.sh";
                break;
            case "dothat":
                $command = "sh /path/to/dothat.sh";
                break;
            default:
                $command = "";
                break;       
        }
        shell_exec("nohup $command &");
        // nohup: run a command immune to hangups, with output to a non-tty 
    }
?>

示例:http://www.my-site.com/bash.php?c=dothis&p=mypass

您也可以通过apache .htaccess/.htpasswd密码保护目录,然后,您可以使用以下命令访问脚本:

http://user: pass@www.my-site.com/bash.php ? c = dothis& p = mypass