create account

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

View this thread on: hive.blogpeakd.comecency.com
· @dappcoder ·
$4.39
MMoSP005[中/English]- crontab run steem-python robot regularly in anaconda3(crontab定时执行anaconda3环境的python版本steem机器人)
Note[备注]: [why is it bilingual(为何双语同文)?](/@dappcoder/mmosp-english-notice-on-the-merger-of-bilingual-courses-or)

为了跨平台开发,所以用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](https://cdn.steemitimages.com/DQmesC12Bkm2KscfcpjJuLEPzecuTQGkJzn5omi2FBUTS8b/0.jpg)
[图片来源:百度搜索(Picture source: Baidu search)](https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3649853129,3563846145&fm=26&gp=0.jpg)
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 44 others
👎  
properties (23)
authordappcoder
permlinkmmosp005-english-crontab-run-steem-python-robot-regularly-in-anaconda3-crontab-anaconda3-python-steem
categoryhive-180932
json_metadata{"tags":["cn","chinese","hive-143316","cn-reader","steemdevs","hive-139531","stem","programming","sct","dblog","whalepower","actnearn","neoxian","palnet","upfundme","zzan","steemleo","busy","wherein","steempreak","esteem","esteem-cn","mediaofficials","marlians","lassecash"],"image":["https://cdn.steemitimages.com/DQmesC12Bkm2KscfcpjJuLEPzecuTQGkJzn5omi2FBUTS8b/0.jpg"],"links":["/@dappcoder/mmosp-english-notice-on-the-merger-of-bilingual-courses-or","https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3649853129,3563846145&fm=26&gp=0.jpg"],"app":"esteem/2.2.2-mobile","format":"markdown","community":"esteemapp"}
created2020-02-28 04:14:18
last_update2020-02-28 04:14:18
depth0
children1
last_payout2020-03-06 04:14:18
cashout_time1969-12-31 23:59:59
total_payout_value2.180 HBD
curator_payout_value2.211 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,073
author_reputation9,480,331,507,174
root_title"MMoSP005[中/English]- crontab run steem-python robot regularly in anaconda3(crontab定时执行anaconda3环境的python版本steem机器人)"
beneficiaries
0.
accountesteemapp
weight300
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id95,897,891
net_rshares17,005,300,723,108
author_curate_reward""
vote details (109)
@esteemapp ·
**Yay!** <br>Your post has been **boosted with ESTM**. Keep up the good work! <br>Dear reader, Install [Android](https://play.google.com/store/apps/details?id=app.esteem.mobile.android), [iOS](https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=1451896376&mt=8) Mobile app or [Windows, Mac, Linux](https://github.com/esteemapp/esteem-surfer/releases) Surfer app!<br>Learn more: https://esteem.app <br>Join our discord: https://discord.me/esteem
properties (22)
authoresteemapp
permlinkre-2020229t17144856z
categoryhive-180932
json_metadata{"tags":["esteem"],"app":"esteem/2.2.2-welcome","format":"markdown+html","community":"esteem.app"}
created2020-02-29 16:14:48
last_update2020-02-29 16:14:48
depth1
children0
last_payout2020-03-07 16:14:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length459
author_reputation420,443,679,514,793
root_title"MMoSP005[中/English]- crontab run steem-python robot regularly in anaconda3(crontab定时执行anaconda3环境的python版本steem机器人)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id95,941,247
net_rshares0