import os
import sys
import time
import signal
from subprocess import Popen, PIPE, DEVNULL
APP_DIR = '/root/Flask'
APP_FILE = 'manage.py runserver'
PID_FILE = '/var/run/Flask_app.pid'
def start_app():
command = f'cd {APP_DIR} && python3.8 {APP_FILE}'
process = Popen(command, shell=True, stdout=DEVNULL, stderr=DEVNULL, preexec_fn=os.setsid)
with open(PID_FILE, 'w') as pid_file:
pid_file.write(str(process.pid))
def stop_app():
if os.path.exists(PID_FILE):
with open(PID_FILE, 'r') as pid_file:
pid = int(pid_file.read().strip())
os.killpg(pid, signal.SIGTERM)
os.remove(PID_FILE)
def restart_app():
stop_app()
time.sleep(1)
start_app()
def status_app():
if os.path.exists(PID_FILE):
with open(PID_FILE, 'r') as pid_file:
pid = int(pid_file.read().strip())
try:
os.kill(pid, 0)
return f"Your app is running (PID: {pid})"
except ProcessLookupError:
return "Your Flask app is not running"
else:
return "Your Flask app is not running"
if __name__ == '__main__':
if len(sys.argv) != 2:
print("Usage: python3.8 your_app_service.py [start|stop|restart|status]")
sys.exit(1)
action = sys.argv[1]
if action == 'start':
start_app()
print("Your Flask app has been started.")
elif action == 'stop':
stop_app()
print("Your Flask app has been stopped.")
elif action == 'restart':
restart_app()
print("Your Flask app has been restarted.")
elif action == 'status':
status = status_app()
print(status)
else:
print("Invalid action. Usage: python3 your_app_service.py [start|stop|restart|status]")
sys.exit(1)flask 服务管理脚本
原创breaklinux 博主文章分类:Python ©著作权
©著作权归作者所有:来自51CTO博客作者breaklinux的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:基础服务启动System管理
下一篇:k8s Pod管理脚本
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
服务管理脚本(SHELL)
服务管理脚本(SHELL)
服务 管理 脚本 -
Java 实现 B/S 架构详解:从基础到实战,彻底掌握浏览器/服务器编程
码示例。文章还总结了最佳实践和常见误区,强调RESTful规范、Token认证、日志记录等关键要点,并附有Java B/S开发
#java #后端 #开发语言 #学习 #个人开发 -
Spark大数据开发与应用案例(视频教学版)(十七)--第十一章
现。
#大数据 #spark #分布式 #数据仓库 #hadoop
















