The Redis connection is not released after PHP starts the PHP script with shell_exec

< H2 > problem description < / H2 >

there are two PHP scripts, start.php and processes2.php
, both of which connect to the same Redis

.

execute procedure:
assume that the number of connections to redis at this time is 0. I used redis-cli info | grep connected_clients to view it. The
command line executes path/to/php start.php , and you will do the following two things in this script:

  1. Connect Redis
  2. start the script with shell_exec processes2.php

then start.php exit
there is only one thing to do in processes2.php , and that is to connect Redis

problem points:
execute start.php before using redis-cli info | grep connected_clients check that the number of connections is 0.
execute start.php , and check the number of Redis connections 1 when connecting to Redis but not starting the script processes2.php .
starts processes2.php in start.php and exits. When processes2.php connects to Redis, the number of connections is 2.
when processes2.php is finished, check that the connection is back to 0.
this means that process 1 starts process 2, process 1 exits after running, and process 2 is running but the connection in process 1 is not released!
start.php after execution, use ps-ef | grep php can no longer be found, and really quit.

the number of connections I have expressed here refers to the number of connected clients displayed on the Redis server. Whether the two processes are connected using the same connection
I am not sure. I do not know how to confirm-_-| |.

< H2 > the way I tried < / H2 >
  • functions exec () , system () have the same effect
  • The last call of start.php has the same effect as exit () and die .
  • I tried using the connetc and pconnect of the Redis plug-in with the same result
  • set the Redis connection to be idle for a certain period of time and then close config set timeout 10 , which can slowly reduce the number of connections. But this is not what I want
  • manually calls close to close the connection before starting processes2.php , which is fine, but it"s not what I want. Because the later business may need to use Redis, then I don"t have to do it again.
< H2 > ask for answers < / H2 >
  • Why is this happening, and what is the principle behind it?
  • Is there any solution for
  • that when start.php exits, its connection will be automatically released,
< H2 > Simulation Code < / H2 > The

code can be copied locally and run. If you are going to upload, you can only paste the

if you find that the file cannot be uploaded.

configuration file

<?php
return [
    "host" => "127.0.0.1",
    "port" => 6379
];

start.php

<?php

$config = include __DIR__ . "/config.php";

echo "process 1 START" . PHP_EOL;
echo "Redis..." . PHP_EOL;

// Redis
$cacheInstance = new \Redis();
try {
    $cacheInstance->connect($config["host"], $config["port"], 0);
} catch (\Exception $exception) {
    echo sprintf("redishost:%s, port:%s, timeout:%s", $config["host"], $config["port"], 0) . PHP_EOL;
    exit();
}
if (!empty($config["password"])) {
    try {
        $cacheInstance->auth($config["password"]);
    } catch (\Exception $exception) {
        echo "redispassword:" . $config["password"] . PHP_EOL;
        exit();
    }
}

echo "Redis " . PHP_EOL;

// Redis 
//php-redisinfo()shell
$clientNum = `redis-cli info | grep connected_clients`;
$clientNum = preg_replace("/[^\d]/", "", $clientNum);
echo "\033[32m 1Redis:\033[37m" . $clientNum . PHP_EOL;

//shell 
echo "22..." . PHP_EOL;
$logFile2 = __DIR__ . "/process2.log";
$processFile = __DIR__ . "/processes2.php";
`nohup /app/php/bin/php {$processFile} > {$logFile2} 2>&1 &`;

echo "2" . PHP_EOL;
echo "2{$logFile2}" . PHP_EOL;

$desc = <<<desc

1
 Reids 2
1redis n
1n+1
2n+2
2n
desc;
echo $desc . PHP_EOL;
echo "exit!" . PHP_EOL;

processes2.php

<?php

$config = include __DIR__ . "/config.php";

echo "2" . PHP_EOL;
echo " Redis..." . PHP_EOL;

$cacheInstance = new \Redis();

// Redis
try {
    $cacheInstance->connect($config["host"], $config["port"], 0);
} catch (\Exception $exception) {
    echo sprintf("redishost:%s, port:%s, timeout:%s", $config["host"], $config["port"], 0) . PHP_EOL;
    exit();
}
if (!empty($config["password"])) {
    try {
        $cacheInstance->auth($config["password"]);
    } catch (\Exception $exception) {
        echo "redispassword:" . $config["password"] . PHP_EOL;
        exit();
    }
}

echo "2 Redis " . PHP_EOL;

$startTime = time();

// Redis 
echo " Redis  60" . PHP_EOL;
$writeFileName = __DIR__ . "/output_from_process2.log";
do {
    //php-redisinfo()shell
    $clientNum = `redis-cli info | grep connected_clients`;
    $clientNum = preg_replace("/[^\d]/", "", $clientNum);
    echo ":" . $clientNum . " waite 1s..." . PHP_EOL;
    sleep(1);
} while ( time() - $startTime < 60 );

$desc = <<<desc

 Redis 
1

2 redis-cli info | grep connected_clients  Redis 

desc;
echo $desc . PHP_EOL;

echo "exit!" . PHP_EOL;
Dec.08,2021
The underlying layer of

PHP's redis extension has a connection pooling mechanism. The connections you open will not be closed immediately at the underlying layer, but will be placed in the connection pool for next use.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e9fd11-1a88.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e9fd11-1a88.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?