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

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

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.
{"cc_re__cc_re__cc_by_sa_3_0": ["                  <li><a href=\"//area51.stackexchange.com\" title=\"proposing new sites in the Stack Exchange network\">Area 51</a></li>\r\n                        <li><a href=\"//careers.stackoverflow.com\">Stack Overflow Careers</a></li>\r\n                    \r\n                </ol>\r\n            </td>\r\n    </tr>\r\n</table>\r\n                </div>\r\n            </div>\r\n\r\n            <div id=\"copyright\">\r\n                site design / logo &#169; 2015 stack exchange inc; user contributions licensed under <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc by-sa 3.0</a> \r\n                with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2015.3.2.2348\r\n            </div>\r\n                <div id=\"additional-notices\">\r\n                    Ubuntu and Canonical are registered trademarks of Canonical Ltd.\r\n                </div>\r\n            \r\n        </div>\r\n    </div>\r\n    <noscript>\r\n        <div id=\"noscript-war"], "copyright__copyright__copyright": ["id of the running script\nkill -9 <pid_of_your_running_script>\nshare|improve this answer\n\nI think you can use your script and when you want to stop it you open a terminal and:\n1. Get the pid of script: ps -ef | grep \"mycript\"\n2. Terminate kill -s 15 PID or kill kill -s 9 PID it.\n\nshare|improve this answer\n\nYour Answer\n\n\u00a0\ndiscard\n\nBy posting your answer, you agree to the privacy policy and terms of service.\n\nNot the answer you're looking for? Browse other questions tagged or ask your own question."]}