CentOS + PHP: Processes limited with shell_exec - php

I have a httpd process running (just apache2), with PHP. In index.php I have the following code in the file:
echo shell_exec("sudo -u apache python3 /root/folder/script.py 2>&1");
Which should execute a a program (which its doing nicely). The program takes ~40 seconds to return something, so it takes a bit. Ive created another python program which is multithreaded. Which makes a request 400 times on the same time to the url, which should start 400 processes.
When I run the command top -c, I see that the CPU is only at 9% while that should be higher. I tried the next command, ps xa | grep apache | wc -l. That returns 101 processes, I've done the command multiple times and its always at 101.
Before I did the command ps xa | grep apache | wc -l, I first made a mistake and tried ps xa | grep apache2 | wc -l, I don't run apache2, but that still gave me the numer 1. So my theory is that 101 - 1 = 100. Which is a too beautiful number.
So this should be some type of maximum / limit / cap. Where can I configure this?
Thanks for your time
===== EDIT =====
I think its queue'ing the requests, while when I login into the server and run the python script manually, the script just normally works without any slow downs etc.

Related

How to pause wget download that was running by PHP's shell_exec()?

I use the following code to download a file from a remote server using wget
shell_exec("wget -O {FILENAME} {FILE_URL}");
if I was using the terminal I could pause the download by hitting ctrl+c
how can I pause wget in php?
What you can do is to get ID of the process. You can do that by running
ps -uax | grep wget | awk '{print $2}'
After that you can get a list of PID of processes that match to grep request.
Then use kill command to send your signal to process. Here is some signals descriptions used by kill, you can read more regarding unix signals.
1 HUP (hang up)
2 INT (interrupt)
3 QUIT (quit)
6 ABRT (abort)
9 KILL (non-catchable, non-ignorable kill)
14 ALRM (alarm clock)
15 TERM (software termination signal)

Running backgrounded script from php exec / shell_exec call is now failing and re-spawning where before it used to work

There is a commonly discussed method of starting a background process in PHP using the exec or shell_exec functionality.
I have had success with this in the past with batch email sending, and sending data to APIs in the background.
In a PHP page that you would call by ajax, you do something like this:
echo 'process running';
shell_exec('/usr/bin/php -q path_to_background_script.php > /dev/null &' );
exit;
The background process normally runs as if called by the owner of the php user like a terminal process.
Recently however, under a FASTCGI system (ea-php56) I have found this method has stopped working.
Instead of one process beginning from one web-request to the calling page, I am getting the background script continually terminating and being re-spawned with a new process id. Interestingly, the only way to stop this continual re-spawning is to disable the line in the calling script that starts the process. The re-spawning stops immediately when you save the calling file without the call to the background script.
This tells me that it is actually the calling script (requested by the browser) which is actually being re-spawned.
This is what the re-spawning looks like from the root terminal. Notice the PID changes everytime I look:
[root#*** public_html]# ps -ef | grep php
*user* 725 1 7 23:53 ? 00:00:00 /opt/cpanel/ea-php56/root/usr/bin/php-cgi /home/*user*/public_html/background-script_exec.php
root 727 32411 0 23:53 pts/1 00:00:00 grep php
[root#dev public_html]# ps -ef | grep php
*user* 757 1 5 23:53 ? 00:00:00 /opt/cpanel/ea-php56/root/usr/bin/php-cgi /home/*user*/public_html/background-script_exec.php
root 759 32411 0 23:53 pts/1 00:00:00 grep php
[root#dev public_html]# ps -ef | grep php
*user* 781 1 12 23:54 ? 00:00:00 /opt/cpanel/ea-php56/root/usr/bin/php-cgi /home/*user*/public_html/background-script_exec.php
I have tried disabling "PHP-FPM service for cPanel Daemons". I have tried 'ignore_user_abort()'. fastcgi_finish_request() function is not available so could not try that. I have tried creating a shell script instead to call the background PHP script, which I call from the calling script - but this also does exactly the same thing.
Apart from disabling the ability to trigger background scripts from a PHP web-page, this new PHP FastCGI behaviour is creating an erratic re-spawning process that does not stop without intervention mentioned above. It has made shell_exec / exec functions unstable!
Problem seems similar to that reported here:
php exec/shell_exec/system/popen/proc_open runs calling script itself infinite number of times on linux
but, suggestion reported here does not help in this case.
This seems to solve the problem. Using the 'php5-cli' instead of 'php'
shell_exec('/usr/bin/php5-cli path_to_background_script.php > /dev/null &' );
I had early tried 'php-cli' and found it did not exist - I did not think to check to see that it was named differently!
If having similar problems, have a look for the php binaries:
>>ls /usr/bin/php*
>>/usr/bin/php /usr/bin/php5 /usr/bin/php5-cli /usr/bin/php-config /usr/bin/phpize
use the one which is for command line, and it should then run properly.
-note that this issue was specifically on a linux cpanel easy-apache fastcgi-php system running on Centos.

Stopping in-built php server on Mac Mavericks - Livecode

I'm developing something in Livecode and I have been experimenting with using Mavericks own in-built php server. I started the server by sending the following command through shell...
php -S localhost:8000
This enabled PHP to run successfully through localhost:8000/
However, I can not work out how to stop/disable PHP now in order to continue testing starting it - when I previously started PHP through the terminal I was able to do ctrl+c to stop php running but since I do not yet know how to do this through my app I get this error instead...
Failed to listen on localhost:8000 (reason: Address already in use)
Anybody know how I can stop it either via the terminal or through my Livecode app? Attempts to stop it through the terminal using just ctrl+c do not work
open a terminal and type:
ps -ef | grep php
it will list the php process with the pid (process id)
something like
$ ps -ef | grep php
501 14263 14133 0 10:25AM ttys001 0:00.21 php -S localhost:8000
501 14355 14265 0 10:25AM ttys002 0:00.00 grep php
The note the number for the line that lists your php process, the second column is your pid
in the example the process id us 14263, kill it:
$ kill 14263
do another ps
$ ps -ef | grep php
501 14358 14265 0 10:26AM ttys002 0:00.00 grep php
$
The process should not be listed anymore

Can't kill PHP script

I am developing some PHP scripts on a Namecheap shared server. I accidentally made a loop which seems to go on indefinitely (or for a very long time), so now I am trying to kill it using SSH.
I have viewed a list of running processes with top, found the misbehaving PHP script, and tried to kill it with kill. However, after I kill it with this command, when I try using the ps, it is still running!
The result of the ps:
PID TTY STAT TIME COMMAND
819520 ? S 0:00 /usr/bin/php /my/php/file.php
I have tried killing the process over and over, but it just won't die!
The SSH is limited, so I can't use commands like killall. What do I do??!
To kill the process you can do the following:
Get the PID with ps -ef
kill it with kill -9 <pid>
A nice reference: When should I use kill -9?
Just for fun, an example:
$ sleep 100 &
[1] 4156
$ ps -ef | grep slee[p]
me 4156 3501 0 10:34 pts/5 00:00:00 sleep 100
$ kill 4156
[1]+ Terminated sleep 100
$ ps -ef | grep slee[p]
$
You can use 'ps' (process status) to get the ID and then use 'kill' to stop it.
http://linux.about.com/library/cmd/blcmdl_kill.htm
try this command. this will stop file from executing.
pkill -f /my/php/file.php

Bash script to kill a process if process count is over X amount

So, I was wondering if there was a way to see if one could use bash to kill a PID of a process if more than X amount spawned. For example:
Let's say we have 10 php processes running with separate PIDs all with the command /usr/bin/php. If it hit 10 processes, would there be a way to kill it with a bash script?
(I'll be having this script run full time in the foreground with a terminal)
IMHO, you're solving the wrong problem. I'd rather make sure that the process in itself couldn't have more than one instance (here's an implementation checklist), and give instructions by some sort of instruction stack.
Not a good idea to kill stuff this way, but this is what you asked.
Try with this one (all on one single line):
j=0; for i in `ps fax | grep '/usr/bin/php' | grep -v grep | awk '{print $1}'`; do let j=j+1; if [ $j -ge 10 ]; then echo "Killing process $i"; kill $i; fi done
Pay attention to what you are doing.
HTH.
This worked for me:
#!/bin/bash
#PROGRAM_NAME=/usr/bin/php
PROGRAM_NAME="gedit"
MAX_INSTANCES=2
CURRENT_INSTANCES=0
while true
do
sleep 1
let CURRENT_INSTANCES=`ps aux | grep -c $PROGRAM_NAME`
if [ "$CURRENT_INSTANCES" -ge "$MAX_INSTANCES" ];then
killall $PROGRAM_NAME
echo "killing program!"
exit 0;
fi
done
#!/bin/bash
program_name=php
let max_instances=10
let poll_interval=60
while true; do
if (( count=$(ps -eocomm | grep -c "^$program_name$") > max_instances )); then
killall "$program_name"
echo "Found $count $program_name processes. Killed."
fi
sleep "$poll_interval"
done
When you kill php processes, you also kill client connections, which might result in unsatisfied customers and lost revenue.
If you're running an apache web server, you can limit the number of http servers/client connections and so the number of php processes.
Look into your apache conf file and look out for:
StartServers
MinSpareThreads
MaxSpareThreads
ThreadLimit
ThreadsPerChild
MaxClients
MaxRequestsPerChild
Depending on how your PHP processes are startet (suphp, fcgi, ...) there might be other options as well.
Other web servers provide similar settings.

Categories