1

I have a PHP-script running on my server via a cronjob. The job runs every minute. In the php script i have a loop that executes, then waits one sevond and loops again. Essentially creating a script to run once every second.

Now I'm wondering, if i make the cronjob run only once per hour and have the script still loop for an entire hour or possible an entire day.. Would this have any impact on the servers cpu and or memory and if so, will it be positive or negative?

ShadyD
  • 11
  • 2
  • I'd love several simultaneous cron jobs that have a *positive* effect on the CPU! If you can create that, you'll be rich! ;-D – deceze Aug 30 '11 at 15:04
  • Hahaha I actually ment a cronjob that executes less often in order to save resources. – ShadyD Aug 30 '11 at 15:35
  • does the loop end after 1 minute? why not just have the cron job run every second and no loop in the script? is there ever really a need to have a cron job that runs every second? what does this cron job do? – dqhendricks Aug 30 '11 at 17:52
  • Yes it is absolutely essential for the script to run every second. My webhost does not support cronjobs executed every second. Once a minute at most. – ShadyD Aug 30 '11 at 20:06

3 Answers3

1

I spot a design flaw.

You can always have a PHP script permanently running in a loop performing whatever functionality you require, without dependency upon a webserver or clients.

You are obviously checking something with this script, any incites into what? There may be better solutions for you. For example if it is a database consider SQL triggers.

Steve-o
  • 12,678
  • 2
  • 41
  • 60
  • Enlighten me please :)! How can you permanently run a PHP script without depending on a server/client? Won't this take up allot of resources? – ShadyD Aug 30 '11 at 20:08
  • Sorry, forgot to add. It's a script that checks the database (MySQL) for updates and inserts records if nessecary. It's technique is comparible to an auction script i suppose. – ShadyD Aug 30 '11 at 20:09
0

In my opinion it would have a negative impact. since the scripts keeps using resources. cron is called on a time based scale that is already running on the server. But cronjob can only run once a minute at most.

Another thing is if the script times out, fails, crashes for whatever reason you end up with not running the script for at max one hour. Would have a positive impact on server load but not what you're looking for i guess? :)

maybe run it every 2 or even 5 minutes to spare server load?

OR maybe change the script so it does not wait but just executes once and calling it from cron job. should have a positive impact on server load.

FLY
  • 2,379
  • 5
  • 29
  • 40
  • That brings up another question, any suggestion how I can check if the script is running? It's absolutely essential for the script to run once per second. – ShadyD Aug 30 '11 at 15:34
  • in that case, I think the best is to create a script that runs on the server that calls your php file. cronjob won't run more than once a second. assuming that you have the possibilities to create such a script. reading the update i would suggest creating a sql trigger see http://stackoverflow.com/questions/1467369/invoking-a-php-script-from-a-mysql-trigger that would help in server load. – FLY Aug 31 '11 at 15:38
0

I think you should change script logic if it is possible.

If tasks your script executes are not periodic but are triggered by some events, the you can use some Message Queue (like Gearman).

Otherwise your solution is OK. Memory leaks can occurs, but in new PHP versions (5.3.x) Garbage Collector is pretty good. Some extensions can lead to memory leaks. Or your application design can lead to hungry memory usage (like Doctrine ORM loaded objects cache). But you can control script memory usage by tools like monit and restart your script when mempry limit reaches some point or start script again when your script unexpectedly shuts down.

Max Romanovsky
  • 2,824
  • 5
  • 30
  • 37