Red
Red

Reputation: 6378

Check php connection status

I have a script that which echo some content and close the connection to the client, However it will work background to process some emails.

Is there any possible way to track the status of the background process ? i mean its completed,or still running etc.

Currently i am setting a flag on DB on process start and process end. But i afraid that some times the script may fail in some conditions.

EX. A force termination from server host.

also is there any possible way to trigger a function when the script stopped execution ( through exit,force stop whatever.. )

I am running the script in windows/linux servers, and both are shared hosts and doesn't have CRON,Task Scheduler support.

Thank you.

Upvotes: 0

Views: 307

Answers (4)

godfrey3835
godfrey3835

Reputation: 126

I didn't test the windows part, so it might be a little buggy. But I think the concept is similar.

$name = 'YOUR_SCRIPT';
if (!isRunning($name)) {
    // script is not running
    doSomething();
}

function isRunning($scriptname)
{
    $output = array();

    if (strtoupper(substr(php_uname(), 0, 3)) === 'WIN') {
        // Windows os
        exec("tasklist | find \"{$scriptname}\"", $output);
    } else {
        // Not window os, maybe linux, freebsd..etc
        exec("ps aux | grep -i $scriptname | grep -v grep", $output);
    }

    return !empty($output);
}

Upvotes: 1

Amar Banerjee
Amar Banerjee

Reputation: 5012

The above answer is right. After that if you want to stop the process you can kill the process you can do it by

kill -9 PID

You will get the PID inside output array. Use that inside if statement.

$output = array();
exec("ps aux | grep -i YOUR_SCRIPT | grep -v grep", $output);
 if (empty($output)) {
   $kill = " kill -9 ".$output['PID'] // Print the output array to get exact index for PID
    exec($kill); 
  }

Upvotes: 0

godfrey3835
godfrey3835

Reputation: 126

In linux server, you can check script status by below code

$output = array();
exec("ps aux | grep -i YOUR_SCRIPT | grep -v grep", $output);
if (empty($output)) {
    // your script is not running
    doSomething();
}

Upvotes: 3

Robert Seddon-Smith
Robert Seddon-Smith

Reputation: 1002

Not entirely sure if I've got the right end of the stick here, but I have assumed that you would like to record the activity of your background process without actually being connected to it.

If so, perhaps the easiest way to do this is to have the process create a txt logfile which you can access whenever you need to to check to see what has been going on. Simply use fwrite() to append the file as required.

You could also use a mysql database table for the same purpose which has the advantage of being easier to read.

If you need to be warned actively if nothing (or something particular) has happened, you could use your header file or connection function to check for you every time a user loads a page. If you have enough hits, you will get an email pdq.

Are you certain you don't have cron? Do check cpanel - it may be hiding there.

Upvotes: 0

Related Questions