Cron eliminates user sessions

1

Scenario:

I'm working on a Xampp / Ubuntu Server web server, I needed a routine to run at least 2 times in a minute (30 seconds approximately).

To achieve this, raise a cron as follows:

* * * * * /opt/lampp/htdocs/control/cronos/cron.sh

This way the cron file was in reach of the web server and FTP and allows me to edit it in the measure.

This cron.sh file has the following code to execute a php script:

#!/bin/sh
cd /opt/lampp/htdocs/control/cronos/
php cron.php

runs every 1 min. Once the cron.php has been executed, the PHP file has the following content:

<?php
$j=0;
while($j<=1){
    $url = 'http://127.0.0.1/index.php';
    $fields = array(
       'idprocess'  => 'p-gen-cronjobs',
       'idform'     => 'p-gen-cronjobs',
    );
    // build the urlencoded data
    $postvars = http_build_query($fields);
    // open connection
    $ch = curl_init();
    // set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, count($fields));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
    curl_setopt($ch, CURLOPT_TIMEOUT, 2);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
    curl_setopt($ch, CURLOPT_FORBID_REUSE, true);     #Update
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
    // execute post
    curl_exec($ch);
    // close connection
    curl_close($ch);
    $j++;
    sleep(25);
}
?>

This php code enters a loop of 25 seconds, which executes 2 request http by POST, sending the 2 variables: 'idprocess' => 'p-gen-cronjobs','idform' => 'p-gen-cronjobs' ; This happens correctly.

The problem:

is that if there is one or more users logged on the same page / platform after some time they are removed from the system, the form of login is through Sessions, I do not know how to check / locate the possible logical error, I verified that when the cron makes the requests validate the cron automatically as it was a user previously logged in (obvious by security issue only if the request was local: 127.0.0.1)

I post this Post in this section of stackoverflow since I consider it a problem in programming.

    
asked by Francisco Núñez 27.09.2017 в 20:08
source

2 answers

0

I do not understand your problem very well, but if you think that your cron is pulling the service you can try it by doing a defined loop let's suppose

 while ($j menorque 10000 ){
  ...
 }

and executing it manually if you pull the service may be exceeding its capacity, if so it may be that a problem is occurring when releasing resources in the "curl_close ($ ch);"

    
answered by 09.10.2017 в 18:22
0

According to your comment, and that it falls after 30 minutes with 12 requests, I think the problem is in the maximum number of clients of the apache which is 256 by default. A possible solution would be to increase them to 512:

MaxClients 512
ServerLimit 512

But the problem would still be persistent, when it reaches the limit of 512 connections, the correct solution would be to modify the code, and add these two lines to close the connection explicitly after connecting to the server:

curl_setopt(CURLOPT_FORBID_REUSE, true);
curl_setopt(CURLOPT_HTTPHEADER, array('Connection: close'));

The documentation for CURLOPT_FORBID_REUSE says:

  

CURLOPT_FORBID_REUSE
  TRUE to force the connection to be closed explicitly upon completion of processing.

And on the other hand the header Connection: close is part of the HTTP / 1.0 specification that tells the server that the connection is not persistent and must be closed.

    
answered by 09.10.2017 в 20:03