I have a script that's scheduled to run periodically on a customers production server to import some data from their FTP server. This needs to run as scheduled, even during the day when the customer is working. Occasionally, the script takes up large amounts of CPU which slows down the customers production environment. I thought I could use cp开发者_C百科ulimit to control the process, but I can't seem to get it to run properly from within PHP. I have a sandbox script with the following lines:
$pid = getmypid();
exec("/usr/bin/cpulimit -p $pid -l 20 -z < /dev/null 2>&1 > /dev/null &");
When I get the pid and run this exact command from the command line it works great. However, when I run the script I get [1]+ Stopped php web/sandbox/sandbox.php
. I have run background tasks from PHP scripts exactly like this many times. What could I be doing wrong?
Thanks in advance,
~ JamesArmescpulimit limits the CPU usage by repeatedly sending the process SIGSTOP and SIGCONT signals. By controlling the timing between these signals, the CPU usage is throttled.
When you run cpulimit manually, you are probably already running the PHP script in the background. The script is actually stopped, but since it's running in the background, your shell thinks you don't care and doesn't report it.
When you incorporate the call to cpulimit in the script itself, the script is running in the foreground, and your shell does report it.
You will see that in both cases cpulimit is actually doing its work, the only difference is the way your shell treats the situation.
What does cpulimit do? It's not a command in any Fedora installation at hand....
It looks like it suspends the process when it accumulates too much CPU time; how about using nice instead?
精彩评论