10

I have a php script that I have been developing which calls an external api within a loop. It is being tested on a VPS which is running LAMP on Debian. I noticed this morning that the api was not responding to my script. When I called the provider, they told me that my server had been calling the api 1000's of times per hour for the past 10 hours. I am assuming that a php script (which I have been working on the day before and testing on my VPS) entered into an infinite loop during one of the executions, and never came out (I have been testing it from the command prompt, and not over the web.) I have attempted to stop and start Apache, but the api support staff says that the calls are still coming in from my server address. How can I find and stop the process? Also, is there a possibility that the Apache stop/start solved the problem, but the api is still trying to sort through past calls?

Please forgive me for not using my local test environment correctly.

Edit: I do not know the process name, I need to discover the name (or pid) based on behavior.

6
  • I'm not an expert here in this particular field, but apache is not php. You should terminate php itself, possibly together with apache. Apache redirects traffic to php. Php executes it and that is sent to your browser etc.
    – LPChip
    Aug 19, 2014 at 21:04
  • 1
    What you need to do to stop PHP depends on how your environment is set up. Since killing apache didn't help, I'd assume it's not running mod_php (which embed PHP's interpreter inside Apache). The other two popular options are php-fpm and php-cgi, so you may want to use ps ax | grep php- to find out which one is running and where. Incidentally, these may have start/stop scripts in init.d (php5-fpm for the former)
    – Powerlord
    Aug 19, 2014 at 21:14
  • Also, for the future, there's a neat, easy, and efficient trick to avoid such extreme amount of outgoing API calls. Simply cache everything (not always possible of course) using something as simple as copy('remotefile', 'temporarylocalfile'); You may run that as a cronjob or just in its own infinite loop with some built-in waiting time. (Or just use any provided utility like wget for this.)
    – Mario
    Aug 19, 2014 at 21:20
  • "Testing from the command prompt" implies to me you were manually running the php binary, either by using php <scriptname> or by using the php shebang at the top of the script. If thats the case, you'll see a php process in ps. Something as simple as pkill -9 php might be all you need. Aug 19, 2014 at 21:27
  • 1
    As a further comment, if it's making outgoing network connections to a external API, those connections will show up in a netstat. You should be able to do a netstat -anop to show you all connections and the PID of the process that controls them. Find out IP of external API server, find IP in netstat list, kill related PID. Aug 19, 2014 at 21:29

2 Answers 2

21

Since you were manually running the php binary, either by using php or by using the php shebang at the top of the script, you'll need to find it's PID and kill it.

You can use pkill -9 php or if pkill itsn't present you can use ps -efw | grep php | grep -v grep | awk '{print $2}' | xargs kill to kill all php processes.

In addition if a process is making outgoing network connections to an external API, those connections will show up in a netstat.

You should be able to do a netstat -anop to show you all connections and the PID of the process that controls them. Find out IP/hostname of external API server, find that IP/hostname in the netstat list, kill related PID.

1
  • 6
    on some system also killall -9 php or kill -9 $(pidof php)
    – Alex
    Sep 11, 2014 at 23:46
4

If i understood correctly your Post, you ran a Script from PHP-CGI through your console that it probably something gone wrong and spammed API's Servers.

So There are many possibilities that may caused this problem. First of all, when you are executing a PHP Script just buy typing "php scriptname.php" it waits for PHP Script to end and then is returning to your main Session Input. If you close your shell during the execution of the script it will stop its operation. Unless you started your PHP with a "screen" or "&" there is not a single possibility the script to continue running.

Now lets analyze its operation. If the script gone into an infinite loop , opening a socket, that means that it would try to connect to the API's Server Like hundred times per second. That means that the target server should ban you from the first 10 seconds. Something you haven't clarified to us is, the flowrate of the connection requests to target server. Does the API's Support provided you any more information about that? And if yes what did they said.

Having said that your VPS still sending requests to the Servers i would suggest you to do

ps -A xa | grep php

and check for running php instances. You might accidentaly started a php script in background by adding a & to the end of the line. If this command return something. Find application's pid, and terminate it with the following command

kill -9 pid

If that doesn work , restart your vps and ask them if it still send them connection requests (something that is currently impossible, and that would mean that their firewall placed your packets in slow mode (meaning that it queued them with a delay to their servers))

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .