Deploying automatic restart Python steem robot on Ubuntu 18.04

in STEEM CN/中文8 months ago

中文版本:《ubuntu18.04 上部署自动重启的 python 程序机器人》

In these days, a python steem robot needs to be deployed remotely. It needs to be online all the time, but it cannot be run by multiple instances at the same time.

0.jpeg
Therefore, it needs to be realized step by step:

  1. Check whether the python steem robot is online regularly. If it is not online, restart it
  2. Regularly check whether there are multiple instances of Python steem robot online at the same time? Close redundant instances

For example, the file of Python steem robot :/home/downvoter/bot.py

Write a controlled shell program:/home/downvoter/bot.sh

#! /bin/bash

source /home/downvoter/.bashrc

case "$(pgrep -f "python /home/downvoter/bot.py" | wc -w)" in

0)  echo "Starting bot:     $(date)" >> /var/log/bot.log
    python /home/downvoter/bot.py &
    ;;
1)  echo "Restarting bot:     $(date)" >> /var/log/bot.log
    kill $(pgrep -f "python /home/downvoter/bot.py")
    python /home/downvoter/bot.py &    
    ;;
*)  echo "Removed double bot and restarting: $(date)" >> /var/log/bot.log
    kill $(pgrep -f "python /home/downvoter/bot.py")
    python /home/downvoter/bot.py &
    ;;
esac

Run it:bash /home/downvoter/bot.sh
Test results meet expectations:
屏幕快照 2020-01-28 下午3.54.19.png