简体   繁体   中英

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. 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

Use this code at the start of the php script:

<?php

ini_set('max_execution_time', 0);

...

Where 0 means to run forever / until the script ends. Or set a reasonable limit in seconds.

This is what I use to run bash scripts via php:

<?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

You can also password protect the directory via apache .htaccess/.htpasswd , then, you can access the script using:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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