1

I have kind of a special requirement that is, I'm using a Raspberry Pi 2 B device to feed some data to a mysql database in a cloud serve. So I have installed LAMP stack on raspberry pi and config my script there. And task of it is insert some values to the remote server (cloud) mysql database (I have opened ports for it). And script should run each 30 seconds.So I do not need to have browser support for this as we do not need to show any details on Raspberry Pi.After first run of the script it should not stop ever , should run 24x7 in all 365 days.

So my question is how to run my script in each 30 seconds, seems I have few options

  1. Using a PHP thread (seems need browser support or first time script running through bash, if some exception occurs in a moment script will stop running it again till it manually reset , database connection will establish one time so time waste for establishing it again and again will prevent)
  2. Using a Cronjob (seems no need of browser support even for first run, guess since it is run complete script again reduce terminate of the script , database connection will establish again and again for each run so that time will waste )

My main concerns are never stop the script , should take minimal time to run the script once and server should not kill by the process as it hit in each 30 seconds. please suggest your opinions sometime there may be an another way which is more efficient than above methods

Chamal Chamikara
  • 496
  • 6
  • 17
  • 3
    cron. or a php process that runs continually. definitely not, ever, a script hit via the browser – pala_ May 28 '15 at 02:37
  • 1
    This question might be better asked on a different forum, e.g. serverfault.com, but there are other answers on stackoverflow, e.g. http://stackoverflow.com/questions/696839/how-do-i-write-a-bash-script-to-restart-a-process-if-it-dies/697064#697064. Or use something like daemontools - http://cr.yp.to/daemontools/faq.html – Clyde May 28 '15 at 02:41

2 Answers2

3

You should create a daemon (like Windows Service) with PHP. Take a look at Kevin's blog that illustrates how to create daemons in PHP.

You can have your daemon sleep for 30s and start over and over again. Do generous logging at first and then add switches/parameters to control the level of logging.

There's a good discussion about daemonizing PHP script on question 2036654 also.

Cron won't cut it for you since its best resolution is 1 minute.

Community
  • 1
  • 1
zedfoxus
  • 35,121
  • 5
  • 64
  • 63
  • Cron can be used with two entries, one waiting 30 seconds, but is very ugly. I would prefer the solution in the link Clyde has given in a comment. – Walter A May 28 '15 at 09:16
0

I recommend to use Cronjob. You can write your PHP script and then setup a Cronjob to run that script every 30 min.

https://askubuntu.com/questions/2368/how-do-i-set-up-a-cron-job

Community
  • 1
  • 1
truongnguyen1912
  • 1,195
  • 8
  • 7