サービスの管理を容易にするために、複数のコマンドのエイリアスを定義しましたが、操作が簡単なグラフィカルインターフェースがまだないため、PHPでシステムコマンドを実行し、単純なクラスをカプセル化できると思いました。正直なところ、私は何年もプログラムを行っており、大きな目標はありませんが、効率ツールを改善するためのコードを書きたいだけです。
それに関連するいくつかの機能を見てみましょう。正直なところ、私は本当に酔っています。オフィシャルがそれほど多くの機能を実行する必要があるのでしょうか?。
exec(string $ command [、array&$ output [、int&$ status]]):string
は外部プログラムを実行し、ステートフルで、結果を保存し、最後の行を返します
passthru(string $ command [、int&$ status]):void
外部プログラムを実行、ステートフル、出力結果
shell_exec(string $ cmd):string
シェル環境でコマンドを実行し、結果をそのまま返します
system(string $ command [、int&$ status]):string
は外部プログラムを実行し、ステータスを持ち、結果を出力し、最後の行を返します
概要:execは完璧で、shell_execは単純です。systemはpassthruに似ており、両方の結果が出力されますが、passthruは戻りません。
レンダリングを見てください
1 <?php 2 3 p(work :: sudo( 'root')-> stop( 'apachectl' )); 4 p(work :: sudo( 'root')-> service( 'mysql')-> start()); 5 6 7 クラスの作業{ 8 9 public $ password = '' ; 10 公開 $ program = '' ; 11 12 public $ command = '' ; 13 公開 $ output = '' ; 14 公開 $ status = 0 ; function __construct($ password = null ){ 18 19 $ this- > password = $ password ; 20 } 21 22 public static function sudo($ password = null ){ 23 24 return new static($ password ); 25 } 26 27 public function exec($ command = null ){ 28 29 if(empty($ command))throw new \ Exception( '命令将空!' ); 30 31 $ this- > command = empty($ this- > password)?$ command: 'echo "'。$ this- > password。 '" | sudo -S '。$ command ; 32 33 exec($ this- > command、$ this- > output、$ this- > status); 34 35 } 36 37 public function service($ program ){ 38 39 if(empty($ program))throw new \ Exception( '服务是空!' ); 40 $ this- > program = 'service'。$ program ; 41 $ thisを返し ます。 42 } 43 44 public function start($ program = null ){ 45 46 if(!is_null($ program )){ 47 $ this- > program = $ program ; 48 } 49 50 $ this- > exec($ this- > program。 'start' ); 51 戻り $ this ; 52 } 53 54 public function stop($ program = null ){ 55 56 if(!is_null($ program )){ 57 $ this- > program = $ program ; 58 } 59 60 $ this- > exec($ this- > program。 'stop' ); 61 $ thisを返す; 62 63 } 64 65 public function restart($ program = null ){ 66 67 if(!is_null($ program )){ 68 $ this- > program = $ program ; 69 } 70 71 $ this- > exec($ this- > program。 'restart' ); 72 $ thisを返す 。 73 74 } 75 76 77 }