skip to Main Content

I got some code in php script (example.php). This script runs about 30-60 seconds and does some actions for one user.

Example.php runs atm only for 1 given user, what is the best way for looping over my users table and running this script for each one?

The script gets called now every ~5 minutes, this should stay the same expect it should be started for each user.

I am running PHP7.2 on my webserver and i tried it already with cronjobs. This works but only for the one hard-coded user. If I would put the loop for all users into this one script the running time would be to enormous. I tried to install pthreads on plesk but this also is a bit complicated.

As said it works for one user already but I dont know whats the best way to let it run for all users.

3

Answers


  1. my solution would be to loop over all your users and call the function from within the loop but if you say that takes to long, i don’t see a way of speeding it up

    i don’t think there is a way to give a user along through a cronjob.
    do you have a code snippet for us to look at?

    Login or Signup to reply.
  2. Currently what I do when I need to run many PHP scripts in parallell is using a queue system like bernard on one hand you will have a launcher script that will push tasks into a queue (implemented for example on a Redis server). This is what would be done by the cron job. Every 5 minutes will call the launcher script, that will push one job per user to the queue, as you can push parameters to the queue, you can push the task with the user as the parameter.

    On the other hand you have as many listeners as you want, the listener processes can be managed by monit for example. Those listeners are waiting for a task to process from the queue independently, whenever they find a task they could do, they would perform it so this way you can peform as many tasks concurrenly as you wish.

    The implementation of what you do at the moment with your user is what would be written in the listener code. This is just a simple way to explain the whole thing, I encourage you to read through the bernard documentation, it would be much clearer.

    Login or Signup to reply.
  3. you can use shell_exec for executing the php file with the various user_id as a parameter.

    shell_exec("/path/to/php /path/to/example.php '".$user_id."' 'alert' >> /path/to/your_log/result.log &");
    

    the symbol & is important because it is tell the server to run this process as background.

    OR

    You can implement some queue system and pushing job as background process.

    Hope it helps.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search