Common functions used to execute linux commands in PHP

Source: Internet
Author: User
Php to execute linux system commands. For more information, see. System functions
Note: execute an external program and display the output information.
Syntax: string system (string command, int [return_var]);
Return value: string

Details:
This function is like the system () function in C, used to execute commands and output results. If the return_var parameter exists, the status after command execution is filled in return_var. It is also worth noting that EscapeShellCmd () can be used to process user input data and prevent users from cracking the system by means of tricks (). If PHP is executed in a modular manner, this function automatically updates the output buffer area of the Web server after each row is output. PassThru () can be used to return complete strings without passing through any other intermediate output interface ().


Instance code:
The code is as follows:
<? Php
$ Last_line = system ('Ls', $ retval );
Echo 'last line of the output: '. $ last_line;
Echo' Return value: '. $ retval;
?>

Exec function
Note: execute external programs.
Syntax: string exec (string command, string [array], int [return_var]);
Return value: string

Details:
This function executes the external program or external command that inputs the command. The returned string is the last line returned after the execution of the external program. to return the complete string, you can use the PassThru () function.

If the parameter array exists, the command adds the array to the parameter for execution. if you do not want the array to be processed, you can call unset () before executing exec (). If both the return_var and array parameters exist, the status after the command is executed is filled in return_var.

It is worth noting that EscapeShellCmd () can be used to process user input data and prevent users from cracking the system by means of tricks ().

Instance code:
The code is as follows:
<? Php
Echo exec ('whoam ');
?>

Popen function
Note: open the file.
Syntax: int popen (string command, string mode );
Return value: integer

Details:
This function executes the command to open a file, which is a file processed in pipelines. Files opened using this function can only be one-way (read-only or write-only), and must be closed using pclose. You can use fgets (), fgetss (), and fputs () in file operations (). If an error occurs when the file is opened, the value false is returned.

Instance code:
The code is as follows:
<?
$ Fp = popen ("/bin/ls", "r ");
?>


PHP monitors linux server load

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 programs that require root permission in Linux, 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.
The code is as follows:
# Include
# Include
# Include
# Include

Int main ()
{
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 the gcc-o w-Wall w. c file. The program w is generated in the current directory. Change the chmod u + s./w owner of this program.
PHP execution

The file content is as follows. put it in the web directory, the access will output the current server load.
The code is as follows:
/*
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.

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.