S
S
Sergey2019-03-02 18:36:45
linux
Sergey, 2019-03-02 18:36:45

Why does cron run three requests instead of one?

Good day. Can you please tell me why cron duplicates the record in the database 3 times in a row?
The essence of cron: Every hour, it monitors the load and sends the result to the database. Every two hours, a graph should be drawn. But cron runs the job 3 times and plots the results 3 times. Accordingly, three identical dates are displayed on the chart.
Here is the cron command (Run every hour):

0 */1 * * * screen -dmS graph_servers_hour bash -c 'cd /var/gsmanager && php cron.php kJ3odY threads graph_servers_hour'

Here is the php code that monitors the cron and sends the results to the database. And also, every two hours sends data to the chart:
<?php
  if(!DEFINED('GSmanager'))
    exit(header('Refresh: 0; URL=http://'.$_SERVER['SERVER_NAME'].'/404'));

  class graph_servers_hour extends cron
  {
    function __construct()
    {
      global $sql, $start_point;

      $servers = $sql->query('SELECT `id`, `online`, `ram_use`, `cpu_use`, `hdd_use`, `date` FROM `servers` ORDER BY `id` ASC');

      while($server = $sql->get($servers))
      {
        if($server['date']+3600 > $start_point)
          continue;

        $sql->query('INSERT INTO `graph_hour` set `server`="'.$server['id'].'",'
            .'`online`="'.$server['online'].'",'
            .'`cpu`="'.$server['cpu_use'].'",'
            .'`ram`="'.$server['ram_use'].'",'
            .'`hdd`="'.$server['hdd_use'].'", `time`="'.$start_point.'"');
      }

      return NULL;
    }
  }
?>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Saboteur, 2019-03-03
@t3g1ng

0 */1 * * * screen -dmS graph_servers_hour bash -c 'cd /var/gsmanager && php cron.php kJ3odY threads graph_servers_hour'

Why */1 and screen if simple enough *
Well, judging by your last question, you are apparently already confusing tasks. Check other tasks. And simplify your notes.

K
ky0, 2019-03-02
@ky0

Why are you running all this in the screen? Why some intermediate "crown tracking script"? I see opportunities for simplification in this architecture.
*/1- redundant composition, just write 0 * * * *.

L
Lev Zabudkin, 2019-12-14
@zabudkin

0 */1
if every two hours, then more precisely:
0 */2
further:
if($server['date']+3600 > $start_point)
continue;
- most likely the out of sync also goes by the time the script is launched by cron and in the script itself, try +3601

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question