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, 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!

1
  • I used to run a chat server using sockets on a linux machine. You can use screen to host the application in a 'screen', if you want to quit, you could simply open up that screen and use control + c Commented May 15, 2016 at 17:57

2 Answers 2

2

Pushing Ctrl+C should stop the running program that is running in the foreground.

You could also kill it when you login in another session an do some ps aux | grep my-php-script.php and see if it is your program, then you can use pkill -f my-php-script.php to kill this process.

Sign up to request clarification or add additional context in comments.

2 Comments

worked like a charm, thanks! oh and you mentioned if I log in with another session. If I already had something working in a background, would killing it be done like you said: pkill -f queuefile.php? or does that only work If I called the php script in the same session?
@Webeng It will work in another session, you just need to be on the same machine and that's all.
0

I understand so that you want to make cron in your server. Therefore you should log in your server via putty and create cron job. For example: After logging...

crontab -e

Then add

1 2 3 4 5 /path/to/command arg1 arg2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.