简体   繁体   中英

Timeout issue when running background PHP script as web

I've got a PHP script that does some heavy lifting that I'm trying to fire off as a background script using the following code:

exec("script.php > /dev/null 2> /dev/null &");

What happens:

  • When I run the code above as part of my web app, the script quits after about a minute.

  • When I run the code as part of my web app without the final ampersand, the job runs fine - but exec() waits for the script to complete before loading the next page, defeating the purpose as the user stares at an unresponsive page.

  • When I run the shell command script.php > /dev/null 2> /dev/null & as myself from the console with the final ampersand, the job runs fine.

  • When I run the shell command from the console as web, the job stops running after about a minute.

I've tried piping my output to logfiles: script.php > /home/public/tmp/output.txt 2> /home/public/tmp/errors.txt & . Output looks normal, I don't get any errors. The script just stops working.

The basic rule seems to be: If run as a foreground process as web or as me, it'll complete. If run as a background process as web, it stops working after about a minute.

I'm not running this as a cronjob because my host (NearlyFreeSpeech) can only run cronjobs once an hour, which is more than I want to make users wait for when the job only takes a couple minutes- it might as well fire when users initiate it.

The subscript starts with set_time_limit(60 * 60 * 4); so this shouldn't be a matter of PHP timing out.

set_time_limit 

does not include shell-execution time. http://php.net/manual/en/function.set-time-limit.php

Try using of the code examples in the comments on that site.

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