PHP執行系統命令的有幾個常用的函數

來源:互聯網
上載者:User

標籤:ima   system   magic   映像   amp   time   unset   命令執行   空白   

先附上測試用的python指令碼
testCsv.py#!/usr/bin/python3# -*- coding: utf-8 -*-import timeimport csvimport randomimport sysfilepath = ‘/Users/magicyou/Desktop/python/temp/‘# 產生csv檔案def exportCsv(title,data):    print(‘test‘)    print(‘test2‘)    time.sleep(10)    fileName = time.strftime(‘%Y%m%d%H%M‘,time.localtime(time.time())) + ‘_‘+ str(int(round((time.time()) * 1000))) + ".csv"    with open(filepath + fileName,"w",newline=‘‘,encoding=‘gb18030‘) as csvfile:         writer = csv.writer(csvfile)        # 先寫入columns_name        try:            writer.writerow(title)            writer.writerows(data)        except:            info = sys.exc_info()             return {‘status‘:False, ‘msg‘:‘檔案寫入失敗!‘}     return {‘status‘:True, ‘fileName‘:hostName + fileName}if __name__ == "__main__":    import time    title = [‘序號‘,‘姓名‘,‘年齡‘,‘價值度‘,‘喜好‘]    data = [[‘序號‘,‘姓名‘,‘年齡‘,‘價值度‘,‘喜好‘],[‘序號‘,‘姓名‘,‘年齡‘,‘價值度‘,‘喜好‘],[‘序號‘,‘姓名‘,‘年齡‘,‘價值度‘,‘喜好‘]]    exportCsv(title,data)
1 system函數說明
string system ( string $command [, int &$return_var ] )

同 C 版本的 system() 函數一樣, 本函數執行 command 參數所指定的命令, 並且輸出執行結果。
如果 PHP 運行在伺服器模組中, system() 函數還會嘗試在每行輸出完畢之後, 自動重新整理 網頁伺服器的輸出緩衝。

參數
參數 描述
command 要執行的命令。
return_var 如果提供 return_var 參數, 則外部命令執行後的返回狀態將會被設定到此變數中。

傳回值
成功則返回命令輸出的最後一行, 失敗則返回 FALSE

# 案例$last_line = system(‘/usr/local/bin/python3 /Desktop/python/python/testCsv.py‘, $output);print_r($last_line);echo $output;// 頁面返回// test// test0
2 exec函數說明
string exec ( string $command [, array &$output [, int &$return_var ]] )

exec() 執行 command 參數所指定的命令

參數
參數 描述
command 要執行的命令。
output 如果提供了 output 參數, 那麼會用命令執行的輸出填充此數組, 每行輸出填充數組中的一個元素。 數組中的資料不包含行尾的空白字元,例如 \n 字元。 請注意,如果數組中已經包含了部分元素,exec() 函數會在數組末尾追加內容。如果你不想在數組末尾進行追加, 請在傳入 exec() 函數之前 對數組使用 unset() 函數進行重設
return_var 如果提供 return_var 參數, 則外部命令執行後的返回狀態將會被設定到此變數中。

傳回值
命令執行結果的最後一行內容。 如果你需要擷取未經處理的全部輸出資料, 請使用 passthru() 函數。
如果想要擷取命令的輸出內容, 請確保使用 output 參數。

# 案例$last_line = exec(‘/usr/local/bin/python3 /Desktop/python/python/testCsv.py‘, $output,$status);print_r($last_line);var_dump($output);echo $status;# 頁面返回// test// array(1) {//   [0]=>//   string(4) "test"// }// 0
3 popen函數說明
resource popen ( string $command , string $mode )

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

參數
參數 描述
command 要執行的命令。
mode 必需。規定串連模式。可能的值:r: 唯讀。w: 唯寫(開啟並清空已有檔案或建立一個新檔案)

傳回值
返回一個和 fopen() 所返回的相同的檔案指標,只不過它是單向的(只能用於讀或寫)並且必須用 pclose() 來關閉。此指標可以用於 fgets(),fgetss() 和 fwrite()。 當模式為 ‘r‘,返回的檔案指標等於命令的 STDOUT,當模式為 ‘w‘,返回的檔案指標等於命令的 STDIN。
如果出錯返回 FALSE。

# 案例$handle = popen(‘/usr/local/bin/python3 /Desktop/python/python/testCsv.py‘, ‘r‘);print_r($handle);echo "‘$handle‘; " . gettype($handle) . "\n";$read = fread($handle, 2096);echo $read;pclose($handle);# 頁面返回// Resource id #2// ‘Resource id #2‘; resource// test
4 passthru函數說明
void passthru ( string $command [, int &$return_var ] )

同 exec() 函數類似, passthru() 函數 也是用來執行外部命令(command)的。 當所執行的 Unix 命令輸出位元據, 並且需要直接傳送到瀏覽器的時候, 需要用此函數來替代 exec() 或 system() 函數。 常用來執行諸如 pbmplus 之類的可以直接輸出映像流的命令。 通過設定 Content-type 為 image/gif, 然後調用 pbmplus 程式輸出 gif 檔案, 就可以從 PHP 指令碼中直接輸出映像到瀏覽器。

參數
參數 描述
command 要執行的命令。
return_var 如果提供 return_var 參數, Unix 命令的返回狀態會被記錄到此參數。

傳回值
沒有傳回值。

# 案例$returnVal = passthru(‘/usr/local/bin/python3 /Desktop/python/python/testCsv.py‘, $status);print_r($status);# 頁面返回// test// 0
5 shell_exec函數說明
string shell_exec ( string $cmd )

通過 shell 環境執行命令,並且將完整的輸出以字串的方式返回。

參數
參數 描述
command 要執行的命令。

傳回值
命令執行的輸出。 如果執行過程中發生錯誤或者進程不產生輸出,則返回 NULL。
Note:
當進程執行過程中發生錯誤,或者進程不產生輸出的情況下,都會返回 NULL, 所以,使用本函數無法通過傳回值檢測進程是否成功執行。 如果需要檢查進程執行的退出碼,請使用 exec() 函數。

# 案例$returnVal = shell_exec(‘/usr/local/bin/python3 /Desktop/python/python/testCsv.py‘);echo $returnVal;# 頁面返回// test
Tip

上述案例都是php在等指令碼執行完畢返回狀態或者結果,怎樣才能只呼叫指令碼不等結果呢?
完整的命令如下:
這個命令好處是當程式報錯,錯誤記錄在log_testPython.txt

python3 /Desktop/python/python/testCsv.py > /temp/test/log_testPython.txt 2>&1 &

原理:該程式的輸出被重新導向到一個檔案或者其它輸出資料流去
你也可以這樣:

當然,這樣是不會擷取到指令碼報錯資訊的

python3 /Desktop/python/python/testCsv.py > /temp/test/null 2>&1 &或者python3 /Desktop/python/python/testCsv.py > /temp/test/null &

PHP執行系統命令的有幾個常用的函數

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.