#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    13
    Rep Power
    0

    Background Process


    Hi, im writing a script that does a lot of image resizing and capturing a few photos from video.

    I am using a background process to run the script.

    My question is, the script at sometimes may run for 12-24 hours at a time.

    Will the script time out if its ran to long? or do background process's run for ever?

    Heres how I start it.

    PHP Code:
    function backgroundProcess($Command$log)
    {
        global 
    $basepath;
        if(
    is_writable($basepath '/admin/logs/'.$log)) {
            
            
    $PID shell_exec("nohup $Command > logs/".$log." & echo $!");
            return(
    $PID);
        } else {
            return(
    "Log file not writable! Cannot start background process! Please chmod 777 or delete /scripts/$log");
        }
    }

    if(
    $_SERVER['REQUEST_METHOD'] == "POST")  {
            
    $query mysql_query("select running from status");
            
    $row mysql_fetch_array($query);
            if (
    $row['running'] == 0) {
                
    $pid backgroundProcess("$php_path $basepath/admin/process-cron.php","process.txt");
                
    $msg "<p>The process is now running $pid</p>"
                } else {
                
    $error "<p>The processor is already running.</p>"
            }


    Thanks
    -Billy
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    It should run indefinitely.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0
    You could setup a cron job? If not, and it must be called within PHP i have once emulated it in a cross platform way without having access to exec() etc by using curl (believe it or not).

    Create the .php script and make it public, and add this to the top:

    if (isset($_POST['key']) == false || $_POST['key'] != 'your secret key') {
    die(); //request not allowed
    }

    then from the other PHP file, create a secure (https) curl connection and POST the secret key to it, set a timeout on curl of say 5 seconds (you can also send a http close connection header from the request page) so that the calling script wont freeze if the request takes to long to complete, in your case 30 seconds.

    This will do the follow: 1. curl will access the page securely (stops just anyway accessing it in their browser) 2. curl will wait 5 seconds, then close the connection (but BOTH your php scripts will carry on)

    It's also compatible regardless of OS internals, shells, etc. You can also tweak the timeouts etc as u see fit.

    Not a brilliant solution but hope it works well enough for you.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    13
    Rep Power
    0
    Awesome thanks for the help guys! I use to use the ASP forums here a lot, I was hoping all my rep would still be here. Oh well

    Thanks again!

IMN logo majestic logo threadwatch logo seochat tools logo