カスタムログファイルタイムスタンプ付きのBashスクリプト

カスタムログファイルタイムスタンプ付きのBashスクリプト

この簡単なスクリプトがあります。

#!/bin/sh
TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
echo "$TIMESTAMP" /etc/init.d/nginx restart >> /usr/local/nginx/logs/mylog.log 2>&1

ただし、カスタムログファイルにのみ時間が印刷され、nginxは再起動されません。私は何を見逃していますか?どこか簡単だと思いますが、わかりません。どんなアドバイス、提案、コメントでも送ってくれてありがとう。よろしくお願いします!

ベストアンサー1

このステートメントは、echo "$TIMESTAMP" /etc/init.d/nginx restart現在の時間と単語の/etc/init.d/nginx合計をrestartログファイルに書き込みます。

nginxを再起動するには、当然別のコマンドで実行する必要があります。

echo "$TIMESTAMP" >>/usr/local/nginx/logs/mylog.log
/etc/init.d/nginx restart >>/usr/local/nginx/logs/mylog.log 2>&1

または、

{ echo "$TIMESTAMP"; /etc/init.d/nginx restart; } >>/usr/local/nginx/logs/mylog.log 2>&1

おすすめ記事