MrTechie
MrTechie

Reputation: 1847

Need cron 'type' approach that runs constantly

I need a decent approach to helping run a cron type script on my shared hosting server. For now - it will have to stay a share hosting account, so that doesn't change. And I understand that a "cron" can do this and set the intervals to be every minute. But I would like to see if we could get it a little faster than that.

I've created a web based application that loops over directories and files within those directories. When a new file is found, it then executes the rest of the stuff that needs to happen. This all works as it should and nothing is needed there. These files that are being processed are files of 'urgency'. They help local departments and people actually respond to these files when they are processed. So the 'urgency' factor is important in here.

With that being said, it would be interesting to hear the best approach to execute this script on a frequent basis, (like every 30 seconds or so) without:

  1. Having the hosting company step in
  2. Eating up too many resources (memory/queries, etc. There's minimal queries happening)
  3. Still allowing this task to be completed regularly without issues.

As a side note: I do have access to multiple accounts on multiple servers and one way I thought about was to creating individual files on those accounts to run as well, and just do something like file_get_contents (even though no output happens) so it would execute the script. Again - the "urgency" is a huge factor in this application so it's important to make this run as fast as possible, but at least allowing for a file to be 'archived' and a couple of queries to run - without overstepping each other in the process (duplications)

I do have access to CLI (shell) if that helps as well.

Upvotes: 0

Views: 44

Answers (1)

Laurence
Laurence

Reputation: 60048

One super simple option - Have the cron run the task every minute:

function task()
{
    doYourThing();
    sleep(30);
    doYourThing();
}

Its not ideal - but it technically runs the doYourThing() every 30seconds.

Edit: why dont you 'trigger' an event when people upload new files to run the task on demand?

Upvotes: 1

Related Questions