0

I was looking for a way to continuously run a PHP script every 15 seconds online, so that I may manage some accounts using an API. I was able to find a script that satisfies what I was looking for as follows:

#!/bin/bash
#This script runs every 15 seconds
#This script is ran in /etc/rc.local (startup)

while (sleep 15 && php test.php) &
do
    wait $!
done 

This script works perfectly and runs every 15 seconds. However, now I want to modify the script such that it may

  1. do what the script is already doing
  2. run a second script, every 5 minutes

Is there a way to modify the current while loop so that I may achieve this? Thanks!

user3745117
  • 127
  • 2
  • 11
  • on point 2 ). You can use crontab to schedule bash scripts. Here are few examples on that. http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/ did not understand point 1. – Devesh Jun 16 '14 at 13:54
  • Edited point 1) for clarity. Thanks for the suggestion, just debating what method I should go with now. – user3745117 Jun 16 '14 at 14:07
  • I thought this link would further help to conclude: http://unix.stackexchange.com/questions/119069/cron-vs-sleep-which-is-the-better-one-in-terms-of-efficient-cpu-memory-utiliz – Sankalp Bhatt Jun 17 '14 at 04:17

3 Answers3

1

If you really want to use a loop

Runs forever until you terminate it, although yours also does.

Change sleep to whatever interval you want, i just set it to 1 for this example.

Set up if statements and use modulus to set the time frame for each one, also probably want to set count back to 0 in the highest timed if to stop it getting too large.

You can add as many as you want for as many times as you want :)

#!/bin/bash

while (true)
do
    sleep 1
    if (( count % 15 == 0 )) ;then
            php test.php
    fi
    if (( count % 300 == 0 )); then
            SecondScript
            count=0
    fi

     (( count = count +1 ))


done
  • I may go with this option depending what I learn about my other options. I am trying to find the best possible solution, but this looks like what I am looking for! Thanks :). – user3745117 Jun 16 '14 at 14:39
  • Definitely a good example of php command line implementation +1 – cujo Jun 16 '14 at 14:49
  • @Jidder tried using this loop, but it's telling me that the counter is not found when running it. Can't seem to figure out where the bug is. – user3745117 Jun 17 '14 at 18:06
  • @Jidder It looks like the script isn't finding the counter: `script.sh: 8: script.sh: count: not found script.sh: 11: script.sh: count: not found script.sh: 16: script.sh: count: not found ` – user3745117 Jun 17 '14 at 18:21
  • Is the script exactly the same as the one i posted ? –  Jun 17 '14 at 18:29
  • @Jidder Does the count variable have to be instantiated prior to the loop? I'm not too familiar with bash. – user3745117 Jun 17 '14 at 18:35
  • You could instantiate it before the loop, but it shouldnt matter as when a variable is called it is set to 0 as far as i know. –  Jun 17 '14 at 18:42
  • @Jidder That's what I assumed. I honestly don't know why the script isn't working in that case. Logic checks out. – user3745117 Jun 17 '14 at 18:52
0

The right way to do such kind of things is via "Cron Job".

The software utility Cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals.

source: http://en.wikipedia.org/wiki/Cron

Useful Source: How to get a unix script to run every 15 seconds?

Community
  • 1
  • 1
Sankalp Bhatt
  • 1,154
  • 13
  • 17
  • I would recommend against it because then you have a false visitor polling the site in order to keep the script alive, PHP isn't designed to do this, see my answer for alternatives – cujo Jun 16 '14 at 13:56
  • I did consider using a CronJob for this type of thing, but I found the script above and it gave me results. Any reason why I should use CronJob over the above script? Thanks. – user3745117 Jun 16 '14 at 14:03
0

There are much better ways to do this because PHP requires and HTTP request to run that file.

  1. Run a cron job to run this request every set interval (not recommended)
  2. Use another program like python which can run a daemon to do this
  3. Do the calculation every time the page is requested (recommended and almost always possible)
cujo
  • 368
  • 3
  • 16
  • Could you detail a bit more about how the example script in the OP should not be used? Sorry, a little bit of a beginner. Could you also detail #3 a little more too? – user3745117 Jun 16 '14 at 14:01
  • 1
    In order to invoke the script you need to have a request going, PHP has a timeout (which causes the thread to close automatically), its not designed to do long running requests. There are few exceptions to this rule like long-polling. As for the third, an example would be like your outstanding notifications, every time you visit a page the website counts them on each request. Most websites do it this way as it is cheaper and doesn't require shell access. Also for notifications generated by other users, you simply generate the messages on their request. – cujo Jun 16 '14 at 14:11
  • Thanks for the response. Just to clarify, the PHP script isn't hosted anywhere and is only running on my local system. The script is intended to read data from a few API requests, and then do a specific action accordingly. Not sure if that changes anything. Thanks again. – user3745117 Jun 16 '14 at 14:16
  • 1
    Yep, no problem, in that case then I would just recommend not using PHP. Python would be a better alternative as it is designed way to better to run as standalone system than PHP. You can also run a python web server in that environment while polling that data. – cujo Jun 16 '14 at 14:20
  • Good information to know. Thanks a lot. I would strongly consider python if it didn't require me to learn the language first haha :). Would a cron job be a valid option in a local script situation? I would like to stick with PHP/bash as much as possible. – user3745117 Jun 16 '14 at 14:34
  • It is, but it is really a duct tape solution in my opinion, plus you will have a cache you will have to clear regularly of html files generated by PHP on every request (If you invoke it by HTTP, im not sure about command line). Look into PHP command line, it will come in useful for invoking php by bash. – cujo Jun 16 '14 at 14:39
  • Thank you, I think I have more than enough information now to choose a solution. I'll choose your answer as my solution, as you listed several options I may go with. Thanks again! – user3745117 Jun 16 '14 at 14:48