【问题标题】:Error in systemd service in linux (Ubuntu)linux(Ubuntu)中的systemd服务错误
【发布时间】:2017-12-24 06:12:08
【问题描述】:

我正在尝试在 Ubuntu 中创建服务,但出现错误。

我在下面写了代码-

FileSystemWatcher.py

import requests
import json
import logging
import sys
import os
import datetime
from watchdog.events import PatternMatchingEventHandler


class DirectoryChangedHandler(PatternMatchingEventHandler):

    patterns = ["*.json","*.csv"]

    logFileName = datetime.datetime.now().strftime('%Y%m%d_log.log')
    script_dir = os.path.dirname(__file__)  # <-- absolute dir the script is in
    rel_path = "log/"+logFileName
    abs_logfile_path = os.path.join(script_dir, rel_path)

    appConfigFilePath = os.path.abspath('config/app_config.json')
    with open(appConfigFilePath) as data_file:
        config = json.load(data_file)
    logging.basicConfig(filename=abs_logfile_path, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.NOTSET)

    def process(self, event):

        print event.src_path, event.event_type
        try:

            filelist = [('files', open(event.src_path, 'rb'))]
            postUrl = self.config["apiBaseUrl"].encode('utf-8')+self.config["fileUplaodApiUrl"].encode('utf-8')
            uploadResponse = requests.post(postUrl, files=filelist)
            if uploadResponse.status_code == 200:
                print "Upload Successful - ", event.src_path
            else:
                print "Upload Failed - ", event.src_path
        except:
            print "Unexpected error:", sys.exc_info()[0]
            pass

    def on_modified(self, event):
        self.process(event)

    def on_created(self, event):
        self.process(event)

workflow.py-

#!/usr/bin/python2.7

import sys
import time
from watchdog.observers import Observer

import FileSystemWatcher as fSystemWatcher


if __name__ == '__main__':
    args = sys.argv[1:]
    observer = Observer()
    observer.schedule(fSystemWatcher.DirectoryChangedHandler(), path=args[0] if args else '.', recursive=True)
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()

    observer.join()

(/lib/systemd/system/FileWatcherSystemd.service)

[Unit]
Description=FileChangeService

[Service]
Type=forking
WorkingDirectory= /home/ashish/Documents/FileSystemWatcher
ExecStart= /home/ashish/Documents/FileSystemWatcher/workflow.py
Restart=on-failure

[Install]
WantedBy=multi-user.target

但我收到以下错误-

FileWatcherSystemd.service - FileChangeService 已加载:已加载 (/lib/systemd/system/FileWatcherSystemd.service;启用;供应商 预设:启用)活动:不活动(死)(结果:退出代码)因为 IST 2017 年 7 月 19 日星期三 10:44:39; 8分钟前进程:6552 ExecStart=/home/ashish/Documents/FileSystemWatcher/FileSystemWatcher.py (code=exited, status=203/EXEC) Main PID: 6552 (code=exited, 状态=203/EXEC)

7 月 19 日 10:44:39 Ashish-PC systemd[1]: FileWatcherSystemd.service: Unit 进入失败状态。 7 月 19 日 10:44:39 Ashish-PC systemd[1]: FileWatcherSystemd.service:失败,结果为“退出代码”。 7月19日 10:44:39 Ashish-PC systemd[1]: FileWatcherSystemd.service: 服务 延迟时间结束,调度重新开始。 7 月 19 日 10:44:39 Ashish-PC systemd[1]:停止 FileChangeService。 7 月 19 日 10:44:39 Ashish-PC systemd [1]:FileWatcherSystemd.service:启动请求也重复 迅速地。 7 月 19 日 10:44:39 Ashish-PC systemd[1]: 启动失败 文件更改服务。

我不知道我在FileSytemWatcher.pyFileWatcherSystemd.service 做错了什么

【问题讨论】:

    标签: python linux ubuntu systemd


    【解决方案1】:

    尝试将您的服务类型更改为简单。所以

    type=simple
    

    您的脚本似乎没有分叉并退出。如果您的服务类型是分叉,systemd 假定已启动的进程分叉,让某些东西在后台运行并退出。它一直等到您的脚本完成运行。

    你的脚本没有退出,systemd 检测到这个并给你超时错误。您的脚本似乎处于无限循环等待键盘中断。这种脚本可以“简单”地运行。这意味着 systemd 只是在后台启动脚本并继续启动序列,而无需等待任何东西。

    【讨论】:

    • 谢谢@Hannu。现在我的服务正在使用 Type=simple
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    • 2017-04-13
    • 2018-04-04
    • 1970-01-01
    相关资源
    最近更新 更多