Start php script in background

Asked

Viewed 548 times

1

I have the.php upload file and I need it to run in the background while my application is used by users. The file send.php instance a pool of Threads that make specific uploads from my system to the world. I believe that the OS (Linux/Freebsd) or even apache have this kind of support already implemented.

  • When will this script start? Does it depend on the user action? Or will select run endlessly on the server?

  • 1

    It will run in the background endlessly.

  • Run it through the shell, with & at the end and redirect the output to some log. a cron is a good output, if it is periodic. Running Apache is extremely problematic, not only because of the timeout issue, but also because it unnecessarily occupies Apache with something that is not meant to serve client pages.

2 answers

3

I had a need much like yours. It needed to perform a background task, return the screen to the user, while the task updated a table in the database with the progress of the process. Explaining a little the solution:

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

Where & at the end causes it to run in the background and "> /dev/null" plays any return on "nothing", not holding the request while the script is run.

I hope I’ve helped

1

I believe the solution you are looking for is a scheduled task (Cron Jobs), read more about how to use here

I hope it helps.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.