简体   繁体   中英

Interacting with a running php process

im wondering if theres a way to run a code in a loop as a process and interacting with it from a different script. I know sockets listen to incoming requests but im referring to internal usage, without requests.

Standard approach:

Use pcntl-signal() and posix-kill() functions to interact by standard or user-defined signals.

Pros:

  • PHP built-in, ready for use functions. No need to reinvent wheels.
  • POSIX compatibility.

Cons:

  • You can only send defined signals to a script. Not values.
  • One-way interaction.

Example of listening script:

<?php
pcntl_signal(SIGTERM, 'sig_handler');
pcntl_signal(SIGUSR1, 'sig_handler');

echo 'Run... PID: ' . getmypid() . PHP_EOL;

$finish = false;
while (!$finish) {
    pcntl_signal_dispatch();
}

echo 'Shutdown...' . PHP_EOL;

function sig_handler($signal) {
    global $finish;
    echo 'Received signal: ' . $signal . PHP_EOL;
    switch ($signal) {
        case SIGTERM:
            $finish = true;
        break;
        case SIGUSR1:
            echo 'Processing SIGUSR1 signal...' . PHP_EOL;
        break;
    }
}

Non-standard approach:

You can implement interaction with script using tools like database, files, sockets, pipes.

Pros:

  • Functionality depends only on realization.

Cons:

  • You need to implement protocol for interaction and support it in your script.

I would say first you set up your script to run in the background. You can implement it yourself (using fork) or use existing libraries. https://www.google.co.jp/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=php%20daemon

Then you define a protocol to communicate. There are many way to implements that, from simple to complex, depending on your needs.

A simple way for example would be to define a folder somewhere in the server that your script reads on the regular basis (loop + sleep). When a file is added, the script reads it, execute the instruction in it, and delete it.

Nope, you cannot directly access another running script, as each script runs on it's own resource sandbox (memory, file descriptors). You'd also need to know it's PID in order to access it, and then after, you'd need some hacking tools to get into that process space.

I have a situation, which is with similar approach. I'm starting independent processes by forking and not wait for them - only start. But I've created control structure in a database. There every children process is storing it's state but also observes a control flag and when this flag is up - then children stops immediately. The children processes stores time-stamped records in another table and that let's me to check what process where is with it's task. In control structure is stored also processes pids and it is possible to send signals to them. I think this can be useful in your situation too.

This may not full fill your requirement. But will works:

Try logging the status to a text file from the first script and read it frequently from the second script to know what is the current status of first script.

when php process starts it take all its requirements for example variables linked pages etc etc.. and show result after it finishes its execution, and after it is completed execution you cant do anything but to re-execute.

its like going to picnic on moon and coming back to home after its over, so no one can disturb you while you are in your picnic. :D

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