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) Copy code The Code is as follows :! #/Usr/local/PHP/bin/PHP
<? 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 just setsProgramPut it in the background, but it does not actually run!
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