Raspberry-pi服务无法运行(代码=退出,状态= 1 /失败)

时间:2019-01-07 08:19:19

标签: service systemd

我试图在Raspberry-Pi 3上运行后台服务以通过http命令控制中继,并在/ etc / systemd / system(gate_controller.service)中创建了服务单元文件:

[Unit]
Description=Gate Controller
After=network.target

[Service]
Type=simple
User=2park
Group=root
WorkingDirectory=/home/pi/2park/Newest/gate_control/
ExecStart=/opt/pythonenv/gate_controller/bin/python2.7 server.py
Restart=on-abort

[Install]
WantedBy=multi-user.target

server.py如下:

#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
Tornado async server as simple REST API
"""
from __future__ import unicode_literals
import json
import tornado.ioloop
from tornado import gen
from tornado.options import options
from tornado.web import RequestHandler, Application
from tornado.httpserver import HTTPServer
from controller import Controller
from log import logger

gate_controller = Controller()


class GateAPIHandler(RequestHandler):

    def prepare(self):
        # Incorporate request JSON into arguments dictionary.
        if self.request.body:
            try:
                json_data = json.loads(self.request.body)
                self.request.arguments.update(json_data)
            except ValueError:
                message = 'Unable to parse JSON.'
                self._return(status=400, message=message)

    def _return(self, message=None, status=200):
        """
        return error response and make sure the response is a json
        """
        self.set_status(status)
        if isinstance(message, dict):
            self.write(message)
        elif isinstance(message, basestring):
            try:
                json_msg = json.loads(message)
            except:
                self.write({'detail': message})
            else:
                self.write(json_msg)
        elif message is None:
            self.set_status(204)
        else:
            self.write({'detail': message})
        self.flush()

    def on_finish(self):
        logger.info("Request ended")

    @gen.coroutine
    def post(self, *args, **kwargs):
        args = self.request.arguments
        action = args.get('action', None)
        if action not in ('open', 'close'):
            self._return(status=400, message="Invalid action")
        else:
            logger.info("Receive %s gate API call", action)
            try:
                if action == 'open':
                    yield gate_controller.open_then_close()
                else:
                    yield gate_controller.close_gate()
            except Exception as e:
                self._return(status=500, message=e.message)
            else:
                self._return()

if __name__ == "__main__":
    logger.info("Start the service")
    tornado.options.parse_command_line()
    app = Application(
        [
            (r'/', GateAPIHandler)
        ]
    )
    server = HTTPServer(app)
    server.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

但是,在运行systemctl status gate_controller.service后,我得到以下消息:

 gate_controller.service - Gate Controller
   Loaded: loaded (/etc/systemd/system/gate_controller.service; disabled)
   Active: failed (Result: exit-code) since Mon 2019-01-07 15:47:40 HKT; 2s ago
  Process: 2007 ExecStart=/opt/pythonenv/gate_controller/bin/python2.7 /home/pi/2park/Newest/gate_control/server.py (code=exited, status=1/FAILURE)
 Main PID: 2007 (code=exited, status=1/FAILURE)

我已在gate_controller.service上尝试使用chmod a + wrx,无济于事地停止/启动/重新启动了该服务...有人知道我在做什么错吗?

0 个答案:

没有答案