PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, February 11, 2022

[FIXED] Run constant number of php scripts on server

 February 11, 2022     cron, lamp, monitoring, php, shell     No comments   

Issue

I am running php scripts in parallel on an AWS lamp server. Right now I have a cron job set up to run each script at 10 min intervals, and then the script is set to exit after running for 20mins. I limit the time each script runs to keep from running out of memory on the server. I have tried to plug the memory holes as best I can, but as I work on this, timing out the script is a good interim solution.

The problem is I get too many scripts running at the same time. So rather than use cron to run the scripts at set intervals, how can I script the server to

A) monitor how many instances of specific php script are running at any given moment

B) if that number drops below n (say 4 instances) the server would fire up another instance of the php script?

Thanks for the help still learning my way around linux!


Solution

The best way would likely be to write your own daemon which runs, forks child processes, monitors them, and will restart them as needed.

Take a look at http://php.net/manual/en/book.pcntl.php

However, in your case, there may be a simpler answer... Put a one minute crontab in place which calls your loader script, like this:

* * * * *  /usr/bin/php /path/to/loader.php

The loader script can be a simple shell script (or php script). Run this command in the shell. It will return the number of instances of the script you are running.

ps aux | grep "/usr/bin/php /path/to/thing/you/run.php" | grep -v grep | wc -l

If # instances > 3, then do not start one. Otherwise, start one. If in PHP, you can simply include the desired file. If in bash, just write /usr/bin/php /path/to/thing/you/run.php

Hope it helps some!



Answered By - gahooa
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing