Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

php app.php

I want to execute it repetitively, but cronjobs not suitable for me. I want to execute again and again when app.php finish working. It won't be executed every X minutes, it will be executed again immediately when it's finish work (like infinity loop). So i will use bash scripting.

while true; do
php app.php
done

this will solve my problem. But usage of while true seems evil. Is it possible to stop this bash script, if i need ? Or can you tell me best way to do this?

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

share|improve this question
    
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

2 Answers 2

up vote 1 down vote accepted

Here is an example about what you can do:

#!/bin/bash

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

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

        read keypress
    done

    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...'
done

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>
share|improve this answer
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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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