著者:Chen
Jinjian個人ブログ:HTTPS://jian1098.github.io
CSDNブログ:https://blog.csdn.net/c_jian
Jane本:https://www.jianshu.com/u/8ba9ac5706b6
連絡先:jian1098 @ qq.com
説明
このスクリプトはLinux
、指定された名前のプログラムが実行されているかどうかを検出できます。実行されていないことを検出した場合は、プログラムを起動してログレコードを作成します。
スクリプティング
vi monitor.sh
次のコードでプログラム名とディレクトリを変更し、コピーして貼り付けて保存します
#!/bin/sh
# 在这修改程序名和程序所在目录,其他不用改
name="entwallet"
path="/root/entwallet"
pid=`ps -A |grep $name| awk '{print $1}'`
now=`date "+%Y-%m-%d %H:%M:%S"`
# 检测是否在运行
if [ ! $pid ]
then
echo "$now $name is not running, start it now..."
# 启动程序命令
cd $path
./entwallet start
new_pid=`ps -A |grep $name| awk '{print $1}'`
# 检测是否启动成功
if [ ! $pid ]
then
echo "$now $name start successfully, pid is $new_pid"
else
"$now $name start failed!"
fi
else
echo "$now $name is running, pid is $pid"
fi
実行権限を追加する
chmod +x ./monitor.sh
スケジュールされたタスクを追加する
crontab -e
必要に応じてスクリプトの実行時間を編集し、ログが保存されているディレクトリを変更します。たとえば、1分に1回確認します。
* * * * * /home/leafserver/monitor.sh >> /home/leafserver/monitor.log 2>&1
入力して保存
ビュー・ログ
tail -f /home/leafserver/monitor.log
ログは次のとおりです
2020-08-11 14:51:22 entwallet is not running, start it now...
2020-08-11 14:51:22 entwallet start successfully, pid is 17117
2020-08-11 14:52:01 entwallet is running, pid is 17117
2020-08-11 14:53:01 entwallet is running, pid is 17117
2020-08-11 14:54:01 entwallet is running, pid is 17117
2020-08-11 14:55:01 entwallet is running, pid is 17117