There is a script that can run for over an hour. The script is launched by ajax on the start button. How can I send a signal to the script so that it stops?

At the end of the script should make the statistics how much worked.

There is an idea to create a file at startup. And check for its availability. If it is, then continue to work. Is there a better way?

if(isset($_GET['stop'])){ unlink('check_file'); return 'deleting!!!!'; } if(!file_exists('check_file')){ fopen('check_file','w'); } for ($i = 0; $i < 10; $i++) { if(file_exists('check_file')){ //Есть другие варианты? //времязатратная задача info('working...'); sleep(1); } else { break; } } info('job stopped. was working: ' . $i . ' seconds'); 

The project does not allow the use of radishes or web sockets.

  • Hardly this is possible in php, look in the direction of nodejs - dev_null
  • one
    bullshit, everything is possible. or create a file or write status in the database - sterx

1 answer 1

I'll try to suggest an algorithm. Create a second route that will accept the request to stop execution. In this second route you create a certain flag (file? Redis? Bd?), And in your long-running cycle you check whether the flag is set. If set, break . And just at the right moment you make a request for the second route, the flag is put in, a break triggered in the long-running cycle.

PS I do not rule out that now someone will remember about web sockets, but if the author of the question did not come to this way, it means that at the current moment this is not our method.