MMoSP005[中/English]- crontab run steem-python robot regularly in anaconda3(crontab定时执行anaconda3环境的python版本steem机器人)

avatar

Note[备注]: why is it bilingual(为何双语同文)?

为了跨平台开发,所以用anaconda3搭建了python的开发环境。
For cross platform development, the development environment of Python is built with anaconda3.

教程中用 steem-python 开发的steem 程序,是需要python运行环境的。
The steem program developed with steem-python in the tutorial requires the python running environment.

当需要定期执行时,需要借助系统的crontab机制,但crontab默认没有相关的环境设置。

当需要定期执行时,需要借助系统的crontab机制,但crontab默认没有 anaconda3 相关的环境设置。
When regular execution is needed, the crontab mechanism of the system is used, but there is no relevant environment setting for anaconda3 by default.

那怎么办?
What can we do?

需要用一个shell程序来执行 steem-python 机器人,由shell程序完成相关环境的设置:
A shell program is needed to execute the steem-python robot. The shell program completes the setting of the relevant environment:

1. steem-python 机器人(`steem-python` robot):crontab_bot
2. steem-python 机器人的 python 文件(`steem-python` python):`/steem/crontab_bot.py`
3. steem-python 机器人的 shell 文件(`steem-python` shell):`/bot/crontab_bot.sh`
4. steem-python 机器人的日志(`steem-python` shell log):`/var/log/crontab_bot.run.log`
5. steem-python 机器人的python日志(`steem-python` python log):`/var/log/crontab_bot.log`
6. anaconda3的安装目录(anaconda3 path):`/root/anaconda3`

那么这个steem-python 机器人的shell文件内容(So the shell file content of the steem-python robot):/bot/crontab_bot.sh

#! /bin/bash
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/root/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/root/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/root/anaconda3/etc/profile.d/conda.sh"
    else
        export PATH="/root/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

export botname="crontab_bot"
export botpy="/steem/$botname.py"
case "$(pgrep -f "python $botpy" | wc -w)" in
0)  echo "Starting $botname:     $(date)" >> /var/log/$botname.run.log
    python $botpy >/var/log/$botname.log 2>&1 &
    ;;
1)  echo "Only one of $botname:     $(date)" >> /var/log/$botname.run.log
#    kill $(pgrep -f "python $botpy")
#    python $botpy >/var/log/$botname.log 2>&1 &    
    ;;
*)  echo "Removed double $botname and restarting: $(date)" >> /var/log/$botname.run.log
    kill $(pgrep -f "python $botpy")
    python $botpy >/var/log/$botname.log 2>&1 &
    ;;
esac

设置每30分钟检测执行一次(Set to check once every 30 minutes to run) :$ crontab -e
*/30 * * * * /bot/crontab_bot.sh >/var/log/crontab_bot.log 2>&1 &

重新启动crontab(Restart crontab):service cron restart
查看crontab状态(View crontab status):service cron status
查看机器人启动日志(View robot startup log):cat /var/log/crontab_bot.run.log
查看机器人执行日志(View robot run log):cat /var/log/crontab_bot.log
0.jpg
图片来源:百度搜索(Picture source: Baidu search)



0
0
0.000
1 comments