Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running background processes after a web request

I'm interested in kicking off processes after a web request, or possibly forking a new process after the initial thread is finished.

I would prefer not to use a cron, because of the nature of the the jobs I'll be running and how often they need to be run, waiting a minute to refresh is not an option.

I'm considering a few ways of doing this:

1) Calling a page in javascript that kicks off the process and immediately returns, then runs tasks after, for instance ajax('/run_jobs.php?job=123').... you get the idea

2) Forking a new thread after a thread has finished; ie output_page(); new thread(); run_job(123); exit();

Anyone have any ideas on the topic or have experience with this.

like image 500
slothstronaut Avatar asked Dec 08 '09 23:12

slothstronaut


1 Answers

Well it depends on the job you want to run. One more complex but more scalable and controllable approach is using Gearman to start and control jobs. The nice thing is you can distribute the jobs to other boxes so your web server won't get the full load.

The easy approach is using exec and nohup like in

<?php
exec("nohup /usr/bin/php script.php >/dev/null 2>/dev/null &");
?>

The important part there is to detach the output channels from the PHP process. For reading the result of the process you might store it in a database.

like image 194
johannes Avatar answered Sep 21 '22 01:09

johannes