3

i want to run a php program constantly in the server which would download some things and store them..

I was thinking of creating a script and running that in cron..

But i wonder is there any other simple method or light weight component which takes less memory in the server while running continuously ??

I thought of another simple thing,

creating a php script with infinite max_execution time and running the code inside a while(true) loop (indefinite loop) with some sleep and then starting the program using php..

Which would be the better available method to do this?

Vijay
  • 5,331
  • 10
  • 54
  • 88

6 Answers6

3

I've created a library called LooPHP which helps abstract out all the while(1) craziness and provides the ability to add event sources (more or less it's a basic runloop you'd fine in GLib, Cocoa, node.js). There's a few of examples which can let you know if you like the style of writing (event based vs loop driven).

I'd suggest reading some of the other questions with PHP and daemon tags. Here is a few of my answers.

But honestly CRON is fairly lightweight and unless it needs to be real-time (or whatever that means to you) writing PHP into a daemon is more work than it's work.

Community
  • 1
  • 1
Kendall Hopkins
  • 43,213
  • 17
  • 66
  • 89
1

Run a cron - that is what they are there for.

Your idea of looping while(TRUE) and sleep()ing is flaky and probably not as clear to any other developers then running a cron job.

alex
  • 479,566
  • 201
  • 878
  • 984
  • Cron is ok but i want to know if there are any other server oriented methods which will use less memory... and if it runs continuously there is no risk of repeating the task again, but there's a chance to repeat while running through cron – Vijay Nov 25 '10 at 05:31
  • @kvijayhari You can fix the cron double up by a shell wrapper. He's a sample of one for rsync, but it could be fixed to work for a PHP script. https://gist.github.com/714960. They that would be executed in the cron instead of the PHP directly. – Kendall Hopkins Nov 25 '10 at 05:38
  • 1
    @kvijayhari I think looping and sleeping will require more memory as it will be one process constantly going. Also if some memory isn't being deallocated properly, your file will begin to leak memory. A cron will start your process, it can run, and then exit, and return the memory to your server. – alex Nov 25 '10 at 05:38
0

using a cron job is pretty good... if you use a script, won't you have to invoke it using a browser, or invoke it inside of a shell every time? If it is to download things, you can set it to run every hour or something like that. If you use it as a script on the webserver, you have the risk of running it already and then invoking it again so there are two instances running at the same time.

nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • that why i ask for a program that runs continuously, if it runs continuously there is no risk of repeating the task again, but there's a chance to repeat while running through cron – Vijay Nov 25 '10 at 05:33
0
  1. Why not SSH into the machine and run php in the console?
  2. If not, do popen() to invoke php-cli in your php script.
  3. Use corntab for programs that need to be run periodically -- a never finish script is never a good thing.
timdream
  • 5,914
  • 5
  • 21
  • 24
0

You can daemonize a PHP script just as you can create Perl or Python daemon. Starting and stopping the script is operating-system dependent. There's a PEAR class System_Daemon that helps with writing a PHP daemon.

leepowers
  • 37,828
  • 23
  • 98
  • 129
0

IF you are just downloading stuff from the internet, wouldn't it be wiser to go with something like wget? On the other hand, if you are crawling/web scraping i guess doing a cronjob would be better

joza
  • 1