ホスティング プロバイダーのプラットフォーム上の CentOs 共有ホスティング パーティション内のすべての WordPress サイトを更新するために、次のコマンド セットを使用しています (毎日の cron 経由)。
wpセット内のコマンドはpushd-popd、WP-CLIプログラムは、WordPress ウェブサイト上のさまざまなシェルレベルのアクションに使用される Bash 拡張機能です。
for dir in public_html/*/; do
if pushd "$dir"; then
wp plugin update --all
wp core update
wp language core update
wp theme update --all
popd
fi
done
ディレクトリはpublic_html、すべての Web サイト ディレクトリが配置されているディレクトリです (通常、各 Web サイトにはデータベースとメイン ファイル ディレクトリがあります)。
public_htmlいくつかのディレクトリがあることを考えるとそうでないWordPress ウェブサイト ディレクトリの場合、WP-CLI はそれらに関するエラーを返します。
これらのエラーを防ぐには、次の操作を実行できると思います。
for dir in public_html/*/; do
if pushd "$dir"; then
wp plugin update --all 2>myErrors.txt
wp core update 2>myErrors.txt
wp language core update 2>myErrors.txt
wp theme update --all 2>myErrors.txt
popd
fi
done
4 回 (またはそれ以上)記述する代わりに2>myErrors.txt、すべてのコマンドからのすべてのエラーが 1 行で同じファイルに送信されるようにする方法はありますか?
答え1
演算子は書き込み用に> fileファイルを開きますfileが、最初に切り捨てます。つまり、新しいファイルが追加されるたびに、> fileファイルの内容が置き換えられます。
すべてのコマンドのエラーを に含めたい場合はmyErrors.txt、そのファイルを一度だけ開くか、>最初の回と>>他の回(ファイルを追加モード)。
pushdここで、 / popderrors がログ ファイルにも送信されることを気にしない場合は、forループ全体をリダイレクトできます。
for dir in public_html/*/; do
if pushd "$dir"; then
wp plugin update --all
wp core update
wp language core update
wp theme update --all
popd
fi
done 2>myErrors.txt
または、たとえば 2、3 より上の fd でログ ファイルを開き、ログ ファイルにリダイレクトする各コマンドまたはコマンド グループに対して、次のように使用できます2>&3(または2>&3 3>&-、不要な fd でコマンドを汚染しないようにします)。
for dir in public_html/*/; do
if pushd "$dir"; then
{
wp plugin update --all
wp core update
wp language core update
wp theme update --all
} 2>&3 3>&-
popd
fi
done 3>myErrors.txt
答え2
中括弧を使うとグループブロックしてすべての出力をリダイレクトします。
for dir in public_html/*/; do
if pushd "$dir"; then
{
wp plugin update --all
wp core update
wp language core update
wp theme update --all
} 2>myErrors.txt
popd
fi
done


