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.

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/

Write a controlled shell program:/home/downvoter/

#! /bin/bash

source /home/downvoter/.bashrc

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

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

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