Implementation of simple database real-time monitoring and dispatching _php techniques via PHP CLI

Source: Internet
Author: User
Tags php cli
Features to implement: monitor the user table and add it to the User2 table if a new record is added. (Practical applications can be a bit deeper, such as data processing, etc.)

Here is the PHP code (dbtest.php)
Copy Code code as follows:

!#/usr/local/php/bin/php
<?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); 3 seconds into the next cycle.
}

The business process should have nothing to say, there are a few places to look at:
The first line is the command path that the PHP CLI mode needs to add, and that while (1) and sleep (3), the rest are normal PHP code.
Through the shell command PHP dbtest.php Run, I tested on the virtual machine, the normal occupancy rate of CPU 0%, memory 1%.
Practical applications can be put in the background to run:
PHP dbtest.php &
BG 1
ps:& command Many places to say is not clear, even wrong. It just put the program into the background, and did not actually run!
Incidentally, review the Linux front and back run related commands

How to use the front and back state of a command

& background Pause added after command

BG background after running with the job number

FG Front desk after operation with the operation number

Ctrl+z background pause (key combination)

Jobs (view all job numbers) command

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.