目录
- systemd管理Flask应用示例
- systemctl常用操作
文档
- https://www.freedesktop.org/software/systemd/man/latest/systemd.unit.html
- Systemd 入门教程:命令篇
- Systemd 入门教程:实战篇
systemd管理Flask应用示例
创建虚拟环境,做好环境隔离
# Python2.7 使用virtualenv创建虚拟环境
$ pip install virtualenv
$ virtualenv venv
$ source ./venv/bin/activate # 进入虚拟环境激活
# Python3+ 使用venv模块创建虚拟环境
app.py
# 安装依赖:pip install flask
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return "Hello world"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)
添加 flask-app.service
# /usr/lib/systemd/system/flask-app.service
[Unit]
Description=Flask App
[Service]
ExecStart=/opt/flask-app/venv/bin/python /opt/flask-app/app.py
Restart=always
[Install]
WantedBy=multi-user.target
需要注意:启动命令要用
绝对路径
systemctl常用操作
# 设置开机启动
systemctl enable flask-app
# 检查开机启动
systemctl is-enabled flask-app
# 启动应用
systemctl start flask-app
# 查看应用状态
systemctl status flask-app
# 重启服务
systemctl restart flask-app
# 重新加载配置文件
$ systemctl daemon-reload
# 查看日志
journalctl -u flask-app -f