简体   繁体   English

使用本地服务器上的php运行bash脚本

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

I know I can run a bash command using php but it times out and i need it to continuously run. 我知道我可以使用php运行bash命令,但是它超时了,我需要它连续运行。 So what I am looking to accomplish is to have this site call my a bash file on my server instead of actually running it in the php 所以我要完成的工作是让该站点在服务器上调用我的bash文件,而不是在php中实际运行它

Use this code at the start of the php script: 在php脚本的开头使用以下代码:

<?php

ini_set('max_execution_time', 0);

...

Where 0 means to run forever / until the script ends. 其中0表示永远运行/直到脚本结束。 Or set a reasonable limit in seconds. 或以秒为单位设置合理的限制。

This is what I use to run bash scripts via php: 这就是我用来通过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 

    }

?>

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

You can also password protect the directory via apache .htaccess/.htpasswd , then, you can access the script using: 您也可以通过apache .htaccess/.htpasswd密码保护目录,然后可以使用以下命令访问脚本:

http://user:pass@www.my-site.com/bash.php?c=dothis&p=mypass 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