對於一個項目,我有一個簡單的python腳本,它提供了一個帶有燒瓶的http界面。劇本本身就像一個魅力。使用daemontools運行python腳本作爲服務
#!flask/bin/python
from flask import Flask,request, Response
import os.path
import json
import sys
import logging
import logging.handlers
from dbMongoManager import saveToMongo
from dbSQLManager import saveToMYSQL
from FailedRequest import FailedRequest
from JSONValidation import validateJSON
app = Flask(__name__)
#create logger
logger = logging.getLogger('werkzeug')
#defines logger file and max size
handler = logging.handlers.RotatingFileHandler('request.log',maxBytes=5000000)
#define logger format
formatter = logging.Formatter("%(asctime)s;%(levelname)s;%(message)s")
handler.setFormatter(formatter)
#add loggerhandler to applications
logger.addHandler(handler)
app.logger.addHandler(handler)
#invoked method on a POST request
@app.route('/',methods = ['POST'])
def add():
"""
This function is mapped to the POST request of the REST interface
"""
print ("incoming POST")
#check if a JSON object is declared in the header
if request.headers['Content-Type'] == 'application/json; charset=UTF-8' and request.data:
print ("passed contentType check")
data = json.dumps(request.json)
#check if recieved JSON object is valid according to the scheme
#if (validateJSON(data)):
saveToMongo(data)
return "JSON Message saved in MongoDB"
raise FailedRequest
#invoked method on a POST request
@app.route('/sql',methods = ['POST'])
def addSQL():
"""
This function is mapped to the POST request of the REST interface
"""
print ("incoming SQL POST")
#check if a JSON object is declared in the header
if request.headers['Content-Type'] == 'application/json; charset=UTF-8':
print ("passed contentType check")
data = request.json
#check if recieved JSON object is valid according to the scheme
if (validateJSON(json.dumps(data))):
saveToMYSQL(data)
return "JSON Message saved in SQLDB"
raise FailedRequest
if __name__ == "__main__":
print "Start App"
app.run(host="0.0.0.0",port=int("80"),debug=True)
由於這只是一個簡單的腳本,它不是很實用,因爲它不會在系統啓動或崩潰時啓動。所以接下來是在我的debian服務器上創建一個監督該腳本的服務。 我對debian整個服務器的東西很陌生,所以我發現通過.conf文件的方法有點令人困惑。作爲簡單的替代方法,我找到了daemontools。 我確實安裝了它,並運行。我創建了/ etc/services中的子文件夾,並放置在run.sh文件,它包含以下內容:
#!/bin/sh
echo Running service
sudo python /home/admin/RestService.py
svscan檢測到它,並創建旁邊有一個監督的文件夾。 儘管我沒有成功啓動restservice.py,但我只能得到一個監督,這意味着我肯定有一些錯誤。
我錯過了什麼或者可能是什麼問題嗎?
謝謝!這幫助我找出了爲什麼我的腳本崩潰了!好的提示 – 2018-02-07 22:50:22