Centos下使用php調用shell腳本


我們在實際項目中或許會遇到php調用shell腳本的需求。下面就用簡單案例在Centos環境下實踐

准備

查看php.ini中配置是否打開安全模式

//php.ini
safe_mode =   //這個如果為off下面兩個就不用管了。我用的是php7,默認沒有當前配置項
disable_functions = safe_mode_exec_dir=

因為safe_mode配置項默認沒有,那么我修改了php.ini中的disable_function選項,把其中一個被禁用的函數去掉,去掉【passthru】函數

disable_functions = exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,fsocket,popen   //這里我去掉了passthru

保存修改,重啟php

測試Shell腳本和php代碼

<?php //php代碼
passthru('/data/wwwroot/default/shell/test.sh',$returnvalue); if ($returnvalue != 0){ //we have a problem!
echo "wrong"; //add error code here
}else{ //we are okay
echo "ok"; //redirect to some other page
}
//shell腳本
#!/bin/bash static_dir="/data/wwwroot/default/test_dir" mkdir $static_dir

php cli模式運行php代碼。目錄創建成功~

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM