Dead Man switch in PHP / Python

So, this is a theoretical question, as language specific, but consider this:

I need PHP to execute a fairly systemic process (using PHP exec();) that will run in the background, but then when the user leaves this page, the process will be killed.

I quickly realized that a dead person switch would be an easy way to implement this, since I am not using any session variables or other server-side variables that might look like this:

if($_SERVER['REQUEST_URI'] !== 'page_with_session.php'){
    //Instead of 'session_destroy();' this would be used to kill said process
}

In any case, a while loop in PHP, a timer reset in a Python script, or a script call again every 15 seconds so that it does not reach the end and kill the process. However, when the user leaves the page, the script will be called, but will not be able to reset before killing the process.

Are there any holes in this idea? If not, what will the implementation look like in PHP / JS? The order I see that it is working on will be:

  • Page hits the user
  • <?php exec('killer.py') ?>
  • killer.py:
    • Listen 20 seconds - If there is no answer ...
    • os.system('pkill process')
  • <?php while(true){sleep(15); exec('killer.py no_wait_dont');} ?>

Any thoughts you guys would be greatly appreciated!

Mason

+5
source share
2 answers

Javascript is much simpler and safer (i.e. not so much).

Just write a ping javascript function that sends something to ping.php every 10 seconds (via ajax). This ping.php will be logged when the last session was received in a user session (e.g. in $_SESSION['last_ping'])

, $_SESSION['last_ping'] . , , , .

+3

JS. , :

  • js
  • php/python pid js
  • js pings php/python pid
  • php/python , pid IPC, , SIGUSR1
  • ; , ..

, , . , , .

+2

All Articles