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 the background and add it to the command
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