How to run the same php script via cron without overloading?

2

What is the best way to run a same php script multiple times by doing random queries for a given column with a limit of 1000 per query of the same mysql table via cron (cpanel) without generating so much overhead?

[cron 1]

*   *   *   *   *   curl -s http://[::1]/dir/arquivo.php

[cron 2]

*   *   *   *   *   curl -s http://[::1]/dir/arquivo.php

[cron 3]

*   *   *   *   *   curl -s http://[::1]/dir/arquivo.php

[cron 4]

*   *   *   *   *   curl -s http://[::1]/dir/arquivo.php

[cron 5]

*   *   *   *   *   curl -s http://[::1]/dir/arquivo.php

[cron 6]

*   *   *   *   *   curl -s http://[::1]/dir/arquivo.php

[cron 7]

*   *   *   *   *   curl -s http://[::1]/dir/arquivo.php

[cron 8]

*   *   *   *   *   curl -s http://[::1]/dir/arquivo.php

[cron 9]

*   *   *   *   *   curl -s http://[::1]/dir/arquivo.php

[cron 10]

*   *   *   *   *   curl -s http://[::1]/dir/arquivo.php
    
asked by anonymous 17.07.2016 / 03:59

1 answer

1

You do not need to create 200 cron entries, to do the same thing, this will affect the performance of your server, causing overload even, unnecessarily, even more using curl .

First, through Notepad, create a shell script, with the following text:

#!/bin/bash
CONSULT=$((($RANDOM %1000) + 1))
$ php -q /path/dir/arquivo.php $CONSULT

Save it with the .sh extension, note that the -q command (indicates that you are sending the request via GET to your URL). >

Now play on your server, with the name of script.sh, give it execute permission on it (if it is through cpanel, just include cron, and ask the administrators for the running configuration, if you have access via ssh, you even can do this):

sudo chmod +x /path/dir/script.sh

Just a single routine, it will do according to the time you set to run, for example, below it will do every 5 minutes:

5 * * * * http://path/dir/script.sh

Now inside your .php file, check the argument passed, $argv , is a reserved variable:

<?php

    if (count($argv)) {
       // o primeiro argumento $argv[0], é o nome do script: arquivo.php
       $limit = $argv[1]; /* captura o segundo argumento
                             passado,que é no caso,
                             o número aleatório de 1 a 1000,
                             representado pela variável $CONSULT do shell */
       $sql = "SELECT * FROM tabela LIMIT 0, {$limit}";
    ...
    
24.10.2016 / 19:38