PHP 調用shell命令

來源:互聯網
上載者:User

標籤:就會   bsp   輸出   安全模式   demo   管道   bash   cmd   例子   

可以使用的命令:

popen
fpassthru
shell_exec
exec
system

 

1.popen

 

resource popen ( string command, string mode )

開啟一個指向進程的管道,該進程由派生給定的 command 命令執行而產生。

返回一個和 fopen() 所返回的相同的檔案指標,只不過它是單向的(只能用於讀或寫)並且必須用 pclose() 來關閉。此指標可以用於 fgets() ,fgetss() 和 fwrite() 。

如果出錯返回 FALSE 。

 

2.shell_exec

 

string shell_exec ( string cmd )

執行cmd命令,命令的結果存在返回結果中;

本函數在安全模式 下被禁用。

 

3.exec

 

string exec ( string command [, array &output [, int &return_var]] )

 

參數:
command
將要執行的命令

output
如果指定了output參數,則對應的array數組將會填滿該命令輸出的每一行。

return_var
如果指定了return_var參數,注意,return_var參數只有output參數被設定後才能指定,那麼該命令的執行狀態將會以結果參數的形式返回給該變數。 


傳回值:
返回改名了的最後一行結果。


範例
例子 1. An exec() example
[[email protected] demo]# vi test.PHP
#!/usr/bin/php  -q
<?php

exec(‘ls /etc‘,$results,$ret);

echo  $results[5] . "/n";
echo  $results[12]. "/n";
echo  $results[3]. "/n";
echo  $ret;
?>

[[email protected] demo]# ./test.php 
aliases
audit
adjtime
0

 

4.system

 

string system ( string command [, int &return_var] )

system是開一個bash進程的,執行完畢之後,就會釋放,如下就會達不到目的:

system("cd /home/y/share/NBHadoop/scripts");

system("sudo -u yahoo ./nb_halfhourly.sh $yes_halfHour");

可以寫成:

system("cd /home/y/share/NBHadoop/scripts;sudo -u yahoo ./nb_halfhourly.sh $yes_halfHour");

PHP 調用shell命令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.