Reputation: 57
Is there a way to kill a PHP program while it's working? I know in Linux/Unix I can do
ps -u [username]
And it will tell me what processes are running. To stop a process I just enter
kill [process #]
With PHP is there any way to see what processes are currently running on the remote server and kill an individual program from completion? Ideally using exec()?
The reason that I ask is that I am on a shared hosting environment and I work on & test my programs directly on the server... Every now and then I'll do something silly like enter a ridiculous number of loops, accidentally, where it can take several minutes to an hour to actually complete.
Seeing that there is virtually no information on the subject, I thought it would be an interesting question to throw out there.
Thanks!
Upvotes: 2
Views: 2031
Reputation: 30881
Ask your hosting provider for SSH access and do that in Unix way. Still you can emulate shell access with a system
function and friends:
exec("ps --no-header -eo pid,user,comm", $output);
foreach ($output as $line) {
$line = preg_split('#\s+#', trim($line));
echo "PID: $line[0] USER: $line[1] PRG: $line[2]\n";
}
Disclaimer: you probably won't be able to kill Apache processes even if you will have shell access.
Upvotes: 1
Reputation: 6052
if u have some infinite loop or something, put this
while(1){
if(!file_exists("continue.txt")){
die("Stop");
}
//Your Code here.
}
so when u delete that file "continue.txt", your script dies.
Upvotes: 1
Reputation: 10996
You can change set_time_limt settings before the risk of a loop, making the script stop after 10 seconds if you wish.
http://php.net/manual/en/function.set-time-limit.php
Upvotes: -1