Home > Backend Development > PHP Tutorial > click.simba.taobao.com Simple real-time database monitoring and scheduling through PHP CLI

click.simba.taobao.com Simple real-time database monitoring and scheduling through PHP CLI

WBOY
Release: 2016-07-29 08:40:21
Original
2561 people have browsed it

Function to be implemented: Monitor the user table. If a new record is added, add it to the user2 table. (Practical applications can be more in-depth, such as related processing of data, etc.)
The following is the PHP code (dbtest.php)

Copy the code The code is as follows:


!#/usr/local/php/ bin/php
mysql_connect('localhost', 'username', 'password');
mysql_select_db("test");
echo 'PID: '.posix_getpid().' '; //Current process PID (under linux)
$old_id = 0;
while (1)
{
$sql = "SELECT `id` FROM `user` ORDER BY `id` DESC LIMIT 1";
$result = mysql_query($sql) ;
$item = mysql_fetch_assoc($result);
$new_id = $item['id'];
$values_arr = array();
for ($i=$new_id; $i>$old_id && $old_id!= 0; $i--)
{
$sql = "SELECT `name`,`age` FROM `user` WHERE `id`='{$i}' LIMIT 1";
$result = mysql_query($sql) ;
$item = mysql_fetch_assoc($result);
$name = $item['name'];
$age = $item['age'];
$values_arr[] = "('{$name}', '{$age}')";
}
if (!emptyempty($values_arr))
{
$values_str = implode(',', $values_arr);
$sql = "INSERT INTO `user2`(`name `, `age`) VALUES {$values_str}";
mysql_query($sql);
}
$old_id = max($old_id, $new_id);
sleep(3); //Enter the next loop after 3 seconds
}


There shouldn’t be much to say about the business process, but there are a few things that need attention:
The first line is the command path that needs to be added in PHP CLI mode, and there are while(1) and sleep(3), and the rest They are all ordinary php code writing methods.
Just run it through the shell command php dbtest.php. I tested it on a virtual machine. Under normal circumstances, the CPU usage is 0% and the memory is 1%.
In actual applications, it can be run in the background:
php dbtest.php &
bg 1
PS: The & command is unclear or even wrong in many places. It just puts the program in the background without actually running it!
By the way, let’s summarize and review the related commands of Linux’s front and backend operations

Commands Front and backend Status Usage

& Background Pause Add after the command

bg Backend Run followed by job number

fg Front Desk Run followed by job number

Ctrl +Z Backstage Pause (Key combination)

jobs (View all job numbers) Command

The above introduces the simple real-time database monitoring and scheduling of click.simba.taobao.com through PHP CLI, including the content of click.simba.taobao.com. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template