在 CentOS 上“远程跑 Python”通常有几种常见场景,下面按使用方式给你讲清楚,你可以看哪种适合你。
ssh user@centos_ip
python3 test.py
或一行执行:
ssh user@centos_ip "python3 /home/user/test.py"
nohupnohup python3 test.py > out.log 2>&1 &
查看输出:
tail -f out.log
screenscreen
python3 test.py
# Ctrl+A D 退出
重新进入:
screen -r
tmux(推荐)tmux
python3 test.py
# Ctrl+B D
扩展:Remote - SSH
连接后可像本地一样跑 Python
from flask import Flask
app = Flask(__name__)
@app.route("/run")
def run():
return "hello"
启动:
nohup python3 app.py > app.log 2>&1 &
crontab -e
示例(每天 3 点):
0 3 * * * /usr/bin/python3 /home/user/test.py
which python3
安装:
yum install python3
chmod +x test.py
如果你能说清楚:
我可以直接给你一套最合适的方案。