I have a PHP script getAnalytics.php that when called, returns a specific integer value. I want to be able to call getAnalytics.php in a bash script that runs continuously checking the PHP script every 30 seconds to see if the value returned has changed.

How would I go about doing this? Or would you recommend a different method?

  • 2
    Why not edit the php script to do a loop for this? – Tim May 11 '19 at 10:00

If you could relax your requirements to a minute between runs, you can easily do this via a cron entry. The advantage of this process is that a program does not simply sit around (and possibly die) causing you to miss some data or window. Also no special steps would be required to start it up between reboots.


If you have no better indication (watching some log file e.g.) to get notification of a possible change just endlessly loop in bash:

while [ 1 ] ; do
   echo "result: $res"
   if [ "$old_result" != "$res" ]; then
     echo "result has changed"
   sleep 30

You will need Ctrl+C to exit. If you want to exit on a particular value (e.g. 42) printed by the script set res to some dummy value at the beginning and then test for [ "$res" != "42" ]

  • I keep getting /myapp/test.php: line 1: 345234: command not found result, – Trey Copeland Dec 14 '14 at 16:58
  • @TreyCopeland what happens when you run /myapp/test.php from the commandline? Does that file have a shebang line? If not it is not an executable script and you have to call it with php /myapp/test.php. – Anthon Dec 14 '14 at 17:21
  • Why while [ 1 ] ; rather than while :; or while true; ? – PM 2Ring Dec 15 '14 at 6:55
  • @PM2Ring Looks better after 30 years of usage? – Anthon Dec 15 '14 at 7:08
  • 1
    @PM2Ring I don't think that speed is an issue if the OP can use PHP. And this way, where to put the exit loop with testing on $res requires less explaining. – Anthon Dec 15 '14 at 7:21

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.