프로그램 감시하다가 다운되면 다시 살리는 쉘 스크립트

in #monitor6 years ago

깃 허브에도 올라가 있는데, 이전에 소개드린 바이넨스 자동 매매 프로그램이 죽었을 때 다시 살리는 쉘 스크립트입니다.
https://github.com/llejo3/binance-trading/blob/master/tradingMonitor.sh

#!/bin/bash

log=./logs/monitor.log
SLEEP_SECONDS=300     # 대기 시간
BASIC_GAP_SECONDS=300 # 비교를 위한 기준 간격 시간

while [ 1 ] 
do
  # 로그가 1분정도마다 남기게 되어 있어서 INFO로 남긴 데이터 중에서 마지막 시간을 가져온다.
  last_date=`tail -200 ./logs/process.log | grep INFO | tail -1 | cut -c 2-20`
  diff_seconds=$(($(date -d now +%s) - $(date -d "$last_date" +%s)))        
  if [ $diff_seconds -gt $BASIC_GAP_SECONDS ]  # 로그가 없으면 다시 실행              
  then
    # process ID를 구한다음 프로세스를 종료시킨 다음 다시 시작한다.
    PROCESS=`ps -ef|grep process|grep -v grep | awk '{print $2}'`
    kill -9 $PROCESS
    wait
    now_date=`date '+%Y-%m-%d %H:%M:%S'`
    echo "$now_date : Process restarted." >> $log
    ./startProcess.sh
    wait
  fi
  sleep $SLEEP_SECONDS
done

Coin Marketplace

STEEM 0.29
TRX 0.11
JST 0.033
BTC 63901.15
ETH 3133.40
USDT 1.00
SBD 4.05