Let's talk about how to execute Linux commands in PHP: http://www.ccvita.com/390.html, which is used to pave the way for this article. In actual project applications, due to the reality of various conditions, using PHP to implement server load monitoring is a more flexible method.
Due to the limited implementation methods of Web server and PHP, it is difficult for us to use PHP in the real environment to call some Linux commands that require the root permission for execution.ProgramIn this regard, I found another way to bypass this restriction from the Internet. First, write a C program to call the system command, and then use PHP to execute the C program.
C program
First, write a c file, such as/usr/local/ismole/W. C.
// Note for WordPress editor reasons, please refer to thisCodeRemove unnecessary spaces in header files
|
# Include<Stdio.H>
|
# Include<Stdlib.H>
|
# Include<Policypes.H>
|
# Include<Unistd.H>
|
| |
IntMain()
|
{
|
Uid_t uid,EUID;
|
| |
// Note to get the current uid
|
UID=Getuid();
|
// Note to obtain the current EUID
|
EUID=Geteuid();
|
| |
// Note exchange the two IDs
|
If(Setreuid(EUID,UID))
|
Perror("Setreuid");
|
| |
// Note: run the Linux Command.
|
System("/Usr/bin/W");
|
Return0;
|
| } |
Compile this fileGcc-o w-wall W. CThe program W is generated in the current directory.
Owner of the programChmod U + S./W.
PHP Execution
The file content is as follows. Put it in the web directory, the access will output the current server load.
<? PHP
|
/*
|
More & original PHP framwork
|
Copyright (c) 2007-2008 ismole Inc.
|
| |
$ ID: servermonitor. php 408 08: 07: 40z Kimi $
|
*/
|
| |
// Note key verification process
|
If ($ key! = $ Authkey ){
|
// Exit ('key error );
|
}
|
| |
$ Last_line = exec ('/usr/local/ismole/W', $ retval );
|
| |
$ Returnarray = explode ("load average:", $ retval [0]);
|
$ Returnstring = $ returnarray [1];
|
| |
| Echo $ returnstring; |
According to the above example, we can use PHP to execute any Linux system commands, SVN updates, server monitoring, backup, recovery, and routine maintenance that we want to execute. For example, use SVN to build a test server: http://www.ccvita.com/383.html.