こんにちは。実行する前に、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
Bashで正しい形式を使用していません。特にと
if
不足している。then
fi
$()
サブシェルはあなたが思うように機能しないかもしれません。終了コード(通常はテストするコード)ではなく、内部コマンドの標準出力を返します。フラグ$(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 ステートメントに関する追加情報。