中文版本:《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:
- Check whether the python steem robot is online regularly. If it is not online, restart it
- 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: