Simple real-time database monitoring and scheduling through PHPCLI

Source: Internet
Author: User
Tags php cli
Continue to the CLI mode test. this time, a simple real-time monitoring and scheduling function of the database is implemented by using the previous "dead loop with latency" method. Function to be implemented: monitor the user table. if a new record is added, add it to table user2. (It can be used in practical scenarios, such as data processing)

Below is the PHP code (dbtest. php)
The code is as follows:
! #/Usr/local/php/bin/php
Mysql_connect ('localhost', 'username', 'password ');
Mysql_select_db ("test ");
Echo 'pid: '. posix_getpid (). ''; // PID of the current process (in 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 should be nothing to say about the business process. There are several points worth attention:
The first line is the command path to be added in the php cli mode, and the while (1) and sleep (3). The rest are common php code writing methods.
Run the shell command php dbtest. php. I tested it on a virtual machine. Normally, the CPU usage is 0%, and the memory usage is 1%.
The actual application can be run in the background:
Php dbtest. php &
Bg 1
PS: & the command is not clear or even wrong in many places. It only puts the program in the background and does not actually run it!
Next, Let's summarize and review the commands for running the front and back ends of linux.

Command frontend and backend status usage

& Pause in the background after the command is added

Bg background operation followed by job number

Fg frontend running followed by job 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.