Shell script--php Execute shell command

Source: Internet
Author: User

Here are just a few common shell commands, some commands that require root user privileges, see: PHP executes shell commands with root privileges

PHP executes shell commands and can use several functions:

String system (string $command [, int & $return _var]) string exec (string $command [, array & $output [, int & ; $return _var]]) void PassThru (string $command [, int & $return _var])

  Note That: These three functions are forbidden by default, if you want to use these functions, you must first modify the PHP configuration file php.ini, Find the keyword disable_functions, remove the function names from this item, and then pay attention to restarting Apache.

first look at the system () and PassThru () two functions are similar, interchangeable:
<?php    $shell = "ls-la";    Echo "<pre>";    System ($shell, $status);    Echo "</pre>";    Note the corresponding relationship between the execution of the shell command and the status value returned    $shell = "<font color= ' red ' > $shell </font>";    If ($status) {        echo "shell command {$shell} failed to execute";    } else {        Echo "shell command {$shell} executed successfully";    }? >

The results of the implementation are as Follows:

  

  Note that system () will display the results immediately after executing the shell command, which is inconvenient because we do not need the results to be output immediately or even output, so we can use the Exec ()

     example of the use of exec ():
<?php    $shell = "ls-la";    EXEC ($shell, $result, $status);    $shell = "<font color= ' red ' > $shell </font>";    Echo "<pre>";    If ($status) {        echo "shell command {$shell} failed to execute";    } else {        Echo "shell command {$shell} executed successfully with the result as follows 

The results of the operation are as Follows:

  

Shell script--php Execute shell 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.