I have a php console app and an Ubuntu server. I'm executing the app with this basic command:

php app.php

I want to execute it repetitively, but cronjobs is not suitable for me. Instead, I want it to execute such that when app.php finishes working it will be executed again immediately (like an infinity loop), rather than being executed again every X minutes. So I made this bash script:

while true; do
php app.php

this solves my problem, but the usage of while true seems evil. Is it possible to stop this bash script, if necessary? Or can you tell me a better way to do this?

(app.php file , fetches a long list of website URLs and their content. It's like a web spider. I connect to the server via ssh , execute bash script with nohup, and then close terminal. )

  • To exit a while loop, use break, or if there are nested loops use break with the number of loops, e.g. break 2 for 2 while loops. You can break on a particular condition, by using an if statement inside the while loop. On what condition do you want the script to break out of the loop? – user76204 Aug 17 '13 at 22:59
  • @Mik , On what condition do you want the script to break out of the loop?it can be lots of things, for example maybe server will be overheat, or we will want to go maintaince, or somethings like these. Because of this i can't use if condition. – Eray Aug 18 '13 at 18:05

Here is an example about what you can do:


while true; do
    if [ -t 0 ]; then stty -echo -icanon time 0 min 0; fi

    while [ "x$keypress" = "x" ]; do
        #do something... in your case:
        php app.php
        #end of 'do something'

        read keypress

    if [ -t 0 ]; then stty sane; fi

    echo "You pressed '$keypress' for pause."
    read -p 'Press [Enter] key to continue or [Ctrl+C] to finish...'

To pause you can press any key without Enter. To continue you must to press Enter. To finish you must to press Ctrl+C.

If you run the script in background using:

nohup <name_of_script> &

you can stop it using the following commands:

ps -ef | grep <name_of_script> #to find the pid of the running script
kill -9 <pid_of_your_running_script>
  • 1
    This script is remarkably similar to this one here at stackoverflow, but perhaps it is a coincidence? – user76204 Aug 17 '13 at 23:06
  • @Mik Do you have a better idea to pause a while loop? – Radu Rădeanu Aug 18 '13 at 5:42
  • See my comment under the OP's question, re using break to escape from a while loop, but I'm not sure on what condition the OP wants the script to break from the while loop. – user76204 Aug 18 '13 at 10:34
  • @RaduRădeanu , i will connect to server via ssh , execute command with nohup, and then close terminal. So this answer won't work for me. – Eray Aug 18 '13 at 18:16
  • @Eray Well, you didn't specified this in your question. – Radu Rădeanu Aug 18 '13 at 18:28

I think you can use your script and when you want to stop it you open a terminal and:
1. Get the pid of script: ps -ef | grep "mycript"
2. Terminate kill -s 15 PID or kill kill -s 9 PID it.

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.