简体   繁体   中英

long running php script hangs

I have this set in my php script to make it supposedly run as long as it needs to to parse and do mysql queries and fetch images for over 100,000 rows.

ignore_user_abort(true);
set_time_limit(0);
#begin logging output
error_reporting(E_ALL);
ini_set('memory_limit', '512M');

I run the command like this in shell:

nohup php myscript.php > output.txt

after running about 8 to 10 hours this script will still be running but execution just stops... no more output.. it's not a zombie process I checked top. It hasn't met the memory limit either and if it did wouldn't it exit?

What is going on? It's a real pain to babysit this script and write custom code to nudge it along. What is going on? I read up on unix maybe cleaning up zombies but it's not a zombie. I know it's not php settings.. and it's not running through a webserver it's from command line only so what gives.

It looks like you haven't detached your process correctly. Currently, if your process's parent die, your process will die too. If you place your process in background (create a real daemon), you'll not meet scuh trouble.

You can execute your PHP this way to really detach it :

php myscript.php > output.txt 2>&1 &

For your information :

> output.txt

will redirect standard output (ie. your echo, print etc) to output.txt file

2>&1

will redirect error output to standard output, writting it in the same output.txt file

&

is the most important thing in your case : it will detach your process to create a real daemon.

Edit : if you're having troubles while disconecting your shell, the most simple is to put your script on a bash script, for example run.sh :

#!/bin/bash
php myscript.php > output.txt 2>&1 &

And you'll run your script this way :

bash run.sh &

In such case, your shell will "think" your program has ended at the end of the shell script, not at the end of the php daemon.

Long-running PHP scripts shouldn't die or hang without reason. I've had scripts that run continuously for 6 months +. There must be something else going on inside of your script body.

I know I should use comment to answer this, but I have not enough reputation to do it...

Maybe your process is consuming 100% of CPU, I had an issue with a while loop without calling a sleep() or usleep() at the end of the loop.

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