0

I am currently following a tutorial that teaches how to create a queue in php. An infinite loop was created in a php script. I simplified the code in order to focus on the question at hand:

while(1) {
        echo 'no jobs to do - waiting...', PHP_EOL;
        sleep(10);  
}

I use PuTTy (with an SSH connection) to connect to the linux terminal in my shared hosting account (godaddy). If I run php queuefile.php in the terminal, I know it will run with no problems (already tested the code with a finite for loop instead of the infinite while loop).

QUESTION: How could I exit out of the infinite loop once it has started? I have already read online the option of creating code that "checks" if it should continue looping with something like the following code:

$bool = TRUE; 
while ($bool)
{
  if(!file_exists(allow.txt)){$bool = FALSE}
  //... the rest of the code

though I am curious if there might be a command I can type in the terminal, or a set of keys I can push that will cause the script to terminate. If there is any way of terminating the script, or if there is a better way to make the previous "check", I would love your feedback!

2

Assuming that you didn't install any signal handlers, you can send SIGINT to the process by pressing Ctrl-C if it in the foreground. If it is not in the foreground, you can use pkill -INT -f '^php queuefile.php$'.

By default, php will quit on receiving SIGINT.

If, for some reason, there is a signal handler installed, you can also try the TERM signal (the default with pkill), and ultimately if you can find no reasonable signal to kill the process with, you can use the (violent and forceful) SIGKILL, by passing -TERM to pkill in place of -INT.

| improve this answer | |
  • OH I see, and If for instance I had something running in the background before having entered the terminal, then performing pkill -INT -f '^php queuefile.php$' would terminate it, even though I had not called the php script to run from the terminal just before atempting to terminate the script? – Webeng May 15 '16 at 18:06
  • @Webeng It would send SIGINT, which may or may not terminate it, but generally the answer is yes. – Chris Down May 15 '16 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.