繁体   English   中英

使用本地服务器上的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=mypasshttp://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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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