1

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?

1
  • 2
    Why not edit the php script to do a loop for this?
    – Tim
    Commented May 11, 2019 at 10:00

2 Answers 2

1

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

res=
while [ 1 ] ; do
   old_result=$res
   res=$(/path/to/getAnalytics.php)
   echo "result: $res"
   if [ "$old_result" != "$res" ]; then
     echo "result has changed"
   fi
   sleep 30
done

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" ]

6
  • I keep getting /myapp/test.php: line 1: 345234: command not found result, Commented Dec 14, 2014 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
    Commented Dec 14, 2014 at 17:21
  • Why while [ 1 ] ; rather than while :; or while true; ?
    – PM 2Ring
    Commented Dec 15, 2014 at 6:55
  • @PM2Ring Looks better after 30 years of usage?
    – Anthon
    Commented Dec 15, 2014 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
    Commented Dec 15, 2014 at 7:21
0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .