実行する前にプロセスを確認してください。

実行する前にプロセスを確認してください。

こんにちは。実行する前に、3つのファイルを調べるスクリプトを作成しようとしています。実行中かどうか。私のコードに問題がありますか?

#!/bin/bash
if [[ ! $(pgrep -f a1.php) ]];  //check if any pid number returned if yes close and exit this shell script    
    exit 1
if [[ ! $(pgrep -f a2.php) ]];  //check if any pid number returned if yes close and exit this shell script 
    exit 1
if [[ ! $(pgrep -f a3.txt) ]];  //check if any pid number returned if yes close and exit this shell script  
    exit 1
else
    php -f a.php; php -f b.php; sh -e a3.txt   //3 files is not running now we run these process one by one
fi

ベストアンサー1

  1. Bashで正しい形式を使用していません。特にとif不足している。thenfi

  2. $()サブシェルはあなたが思うように機能しないかもしれません。終了コード(通常はテストするコード)ではなく、内部コマンドの標準出力を返します。フラグ$(pgrep -c -f a1.php) -gt 0を使用して-c一致するプロセスの数を返すか、終了pgrep -f a1.php > /dev/nullコードを使用することをお勧めします。

    [[ ! $(pgrep -f a1.php) ]]この場合は機能できますが、複数の[[ $(pgrep -f a1.php) ]]プロセスが一致すると失敗するため、脆弱です。

努力する、

if [[ $(pgrep -c -f a1.php) -gt 0 ]]; then
    exit 1
fi
if [[ $(pgrep -c -f a2.php) -gt 0 ]]; then
    exit 1
fi
if [[ $(pgrep -c -f a3.txt) -gt 0 ]]; then
    exit 1
fi

php -f a.php; php -f b.php; sh -e a3.txt

または他のオプション

pgrep -f a1.php > /dev/null && exit 1
pgrep -f a2.php > /dev/null && exit 1
pgrep -f a3.php > /dev/null && exit 1

php -f a.php; php -f b.php; sh -e a3.txt

バラよりhttp://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.htmlif ステートメントに関する追加情報。

おすすめ記事