tags:

views:

68

answers:

2

hello, i need to restart my php program when the memory of the program exceeds 500 mb ... it can be done by any script in php or in shell ????

+1  A: 

Run your PHP script like this:

php -d "memory_limit=500M" myscript.php

And do it forever:

while true; do php -d "memory_limit=500M" myscript.php; done
strager
heh... good trick :)I would use daemon for this...
confiq
i have written an shell script start.sh like thisphp -d "memory_limit=500M" myscript.phpwhile true; do php -d "memory_limit=500M" myscript.php; doneonce i run the program i am getting segmentation fault error....
Ram
@Ram, The script should only contain the second line. I was just showing the natural progression to the solution in my answer. I don't see how you would get a segfault; this would be a bug in PHP which you may want to report. (How are you normally executing the PHP script?)
strager
@strager ,it didnt restart the program the memory is still increasing.... if i use "while true; do php -d "memory_limit=500M" myscript.php; done"
Ram
@Ram, When PHP reaches the memory limit, it raises a fatal error and quits the PHP script. Are you absolutely sure PHP is using more than 500MB?
strager
How does your PHP script hit 500 MB of memory usage so quickly?
BoltClock
A: 

Whenever I deal with long-running programs, I like to include some basic tools for checking whether they are active, what errors they are creating, etc. Here's a quick script I run with cron to make sure that my gearman workers are running.

$program = 'program.php';  

// Run the cmd to get list of running programs  
exec( "ps aux | grep $program | awk '{print $12}'", $output );  

if( count($output) == 0 )  
    exec( "php $program > /dev/null &" );

Of course, you may not want the output of your script sent to /dev/null. In fact, if you're having problems with memory usage, you may want to use the >> operator to capture your script's output in a makeshift log.

Chris Henry