Can't kill PHP script - php

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

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 php script in the background

I have this simple sample script:
#!/usr/bin/php
<?php
while(true) {
error_log("hello " . time() . "\n", 3, "logs.log");
sleep(3);
}
Which I execute with the command:
me:~/Desktop$ php worker.php &
As expected it returns me 6683: the id of the process. Fine.
Now to make sure everything is fine I do a ps and get:
PID TTY TIME CMD
5561 pts/1 00:00:00 bash
6683 pts/1 00:00:00 php
6705 pts/1 00:00:00 ps
[1]+ Stopped php worker.php
Why is it stopped? Is it not supposed to run continuously untill someone kill -9 it?
If I do a second ps I get:
PID TTY TIME CMD
5561 pts/1 00:00:00 bash
6683 pts/1 00:00:00 php
7395 pts/1 00:00:00 ps
The process is there but it is not logging (dormant?)
Anyone?
Ta
The script must have a place where to dump stdout or else it shows the above behaviour (gets stopped). This can be done like this:
php worker.php > /dev/null &
Like this it works and the timed infinte loop does its job. The only problem is: if you now stop the parent process (the shell's) then the background php process stops too.
To get around it run:
disown -h php-process-id
Youi can now close all the shells, log out and go to the pub.
Well done me! ;)
Not sure if my reasoning is correct, but I've run into this problem, too, and solved it by using nohup.
My reasoning was this:
A PHP process has a handle on stdin. By running a process on the background, you can't read from the stdin, perhaps at some point a hangup signal might be sent.
By using nohup, you can ignore that signal.
Here's a few extra words of explanation on the matter

how to kill a passthru process upon the kill of a php script?

I'm running a linux command through PHP passthru(), example:
<?php
file_put_contents("script.pid", getmypid());
passthru("sleep 500", $exit);
Now say I want to kill the script. I would kill the process listed in script.pid; however, this leaves the sleep 500 (or any process) running in the background. I get the same result from system() and shell_exec() although I am using passthru() for the exit return variable. Anyone know a solution or even a reason of why this leaves the process running in the background?
Function passthru() spawns a shell to run your command and then blocks until the passthru process returns. Those are independent processes with different Process IDs than the php interpreter running your script. You can kill the script but you won't kill the processes it started.
However the spawned processes have the same Process Group ID (PGID) and you can use that to kill them or sent them any other signal. The PGID in our case would be the same as the Process ID (PID) of the php script.
To see the PGIDs you can execute the command: ps axjf and you will get something like:
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
24077 12484 12484 24077 pts/9 12484 S+ 1000 0:00 | \_ php sleepScript.php
12484 12486 12484 24077 pts/9 12484 S+ 1000 0:00 | \_ sh -c sleep 500
12486 12487 12484 24077 pts/9 12484 S+ 1000 0:00 | \_ sleep 500
the PGID in our example is 12484 (same as the PID of the php script) and to send to that group a terminate signal, use the kill command with a negative sign in front of the PGID, i.e.:
kill -15 -24077
and you will have terminated all three processes.

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.

How do I stop a script running in UNIX?

I ran a php script, let's use "mytestscript.php" for example.
It will run continuously for a few hours. How can I stop it from the Terminal (UNIX) command line?
Assuming it's running in the background, under your user id: use ps to find the command's PID. Then use kill [PID] to stop it. If kill by itself doesn't do the job, do kill -9 [PID].
If it's running in the foreground, Ctrl-C (Control C) should stop it.
Read the documentation on the ps command and familiarize yourself with its options. It's a very useful command.
You can try using ps -e | grep php to find all processes that have 'php' in their name. After that, you can do kill <PID>, replacing <PID> with the number the ps command gave you.
If you want an automated solution try this -
1.Create a new shell script - vi killer.sh
2.Add the following
#!/bin/bash
while true
do
sleep $1
ps -ef | grep mytestscript.php | grep -v grep | awk '{print $2}' | xargs kill -9
done
3.Grant executable permissions chmod +x killer.sh
4.Execute your script as nohup ./killer.sh <time to sleep before killing> &
5.Leave it and go to the beach!
You can always find the process id of the running process. "ps -ef | grep mytestscript.php". Look at the output and note down pid of the process. use kill pid to kill the process.

Categories