Simple database real-time monitoring and scheduling _php tutorial 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 application can be a bit deeper, such as data related processing, etc.)

Here is the PHP code (dbtest.php)
Copy CodeThe 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); 3 seconds to go to the next loop.
}

The business process should have nothing to say, there are several places to be aware of:
The first line is the command path that the PHP CLI mode needs to add, as well as the while (1) and sleep (3), and the rest are normal PHP code notation.
Through the shell command PHP dbtest.php Run, I test on the virtual machine, the normal occupancy rate of CPU 0%, memory 1%.
Real-world applications can be run in the background:
PHP dbtest.php &
BG 1
ps:& command Many places are very unclear, even wrong. It just puts the program in the background, and it doesn't actually run!
By the way, review the Linux front and back run related commands

How to use the command and back table status

& Background pause added after command

BG background run followed by job number

FG foreground operation followed by operation number

CTRL + Z background pause (key combination)

Jobs (view all job numbers) command

http://www.bkjia.com/PHPjc/320328.html www.bkjia.com true http://www.bkjia.com/PHPjc/320328.html techarticle features to implement: monitor the user table and add it to the User2 table if a new record is added. (Practical application can be a bit deeper, such as data related processing, etc.) the following is the PHP generation ...

  • Related Article

    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.