Home > Backend Development > PHP Tutorial > How to execute shell script in php

How to execute shell script in php

WBOY
Release: 2016-07-29 09:16:03
Original
1513 people have browsed it

This article introduces how to directly execute shell scripts in php. Friends in need can refer to it.

code is as follows: 代 Copy code

code example: & lt ;? php $ message = shell_exec ("SUDO/usr/local/nginx/sbin/nginx- t 2>&1");echo "Run result:".$message."
";

?>



The above code directly executes the SHELL statement in PHP.
If there is a return value, 2>&1 is required. It is responsible for redirecting the output strerr to the strout output.
Otherwise the return result will be empty.


You can use system, exec and the like to execute shell commands under PHP, but each individual system command calls the shell separately,

The environment must be re-initialized every time. Therefore, if you use PHP to execute multiple commands in succession, you cannot implement functions through multiple systems.

This article provides the following two methods for your reference.

1, use php to generate a shell file and execute it, delete it after completion

Copy the code

Code example:file_put_countents('tmp.sh',"cd /usr/ localecho 'string'ls $callback");

system('tmp.sh');



For multiple commands, just change the line.

2, multiple commands are written directly in one statement, as follows:

Copy code

Code example: system("cd {$path1};if [ ! - d {$path2} ];then n { mkdir -p {$path2} n chmod -R 777 {$path2} n } n fi;find -name '{$etaskid}-{$sid}*' -print | while read na

Copyright statement: This article is the original article of the blogger and may not be reproduced without the permission of the blogger

. The above introduces the implementation method of executing shell script in PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template