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

Asked

Viewed 832 times

2

What is the best way to run the same php script several times by making 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
  • 3

    The first thing is to start running this url from there. If you saw this as an example in some post here by Sopt for access to the local machine, let us know what it was so we could take a look, because it’s absurd. PHP has the command line executable that is much more suitable for this (and will not be locking web server task). Take an example here: http://answall.com/a/56171/70 and another one here: http://answall.com/a/124519/70 - About the fact that there are 10 cron entries, I can’t imagine a reason for this. If you can elaborate better, it might help clarify the question.

  • Hi @Bacco, thanks for your willingness to help. 1. I ask you to look at the url :) I used /usr/bin/php before investing in Curl. Several places suggest these two methods, besides Lynx and wget. But what I’m trying to figure out is how to avoid or mitigate the overhead on the server. If that’s possible. The idea of having ten entries is to be able to process 1,500,000 lines per day with the forecast to double this soon...

  • 2

    This is usually for accessing third party stuff. Locally if someone uses Lynx, wget and Curl having the PHP executable, better distrust. Simply you will be occupying the page server, and forcing PHP to have timeout.

  • But @Bacco, besides changing this stop of Curl, normal run n times the same script that will pull randomly the records of a seller among dozens for me to be able to process faster all records?

  • The more crontab entry, the worse it is. It will only be competing between the side tasks. If there’s something time consuming in your php, that’s where you’ll have to fix it. But with what was posted in the question, there’s not much to say. And you need to see if PHP is the right tool for what you want to do.

1 answer

1

You don’t need to create 200 cron entries, to do the same thing, it will affect your server’s performance, causing even unnecessarily overload, even more using Curl.

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

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

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

Now play on your server, with the script name.sh, give permission to run on it (if you go through Cpanel, just include cron, and ask administrators to run configuration, if you have access via ssh, you can do it yourself):

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

Just a single routine, it will do according to the time you set to run, 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}";
    ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.