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

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

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默认没有 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/`
3. steem-python 机器人的 shell 文件(`steem-python` shell):`/bot/`
4. steem-python 机器人的日志(`steem-python` shell log):`/var/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/

#! /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"
    if [ -f "/root/anaconda3/etc/profile.d/" ]; then
        . "/root/anaconda3/etc/profile.d/"
        export PATH="/root/anaconda3/bin:$PATH"
unset __conda_setup
# <<< conda initialize <<<

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

设置每30分钟检测执行一次(Set to check once every 30 minutes to run) :$ crontab -e
*/30 * * * * /bot/ >/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/
查看机器人执行日志(View robot run log):cat /var/log/crontab_bot.log
图片来源:百度搜索(Picture source: Baidu search)

Comments 1

Your post has been boosted with ESTM. Keep up the good work!
Dear reader, Install Android, iOS Mobile app or Windows, Mac, Linux Surfer app!
Learn more:
Join our discord:

29.02.2020 16:14