当前位置: 首页 > 知识库问答 >
问题:

linux(Ubuntu)中systemd服务出错

皇甫飞宇
2023-03-14

我写了下面的代码-

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 loaded:loaded(/lib/systemd/system/FileWatcherSystemd.Service;enabled;vendor Preset:enabled)Active:inactive(dead)(结果:exit-code)自周三2017-07-19 10:44:39 IST以来;8min前进程:6552 execstart=/home/ashish/documents/filesystemwatcher/filesystemwatcher.py(代码=exited,status=203/exec)主PID:6552(代码=exited,status=203/exec)

7月19日10:44:39 Ashish-PC SystemD[1]:FileWatcherSystemd.Service:单元进入失败状态。7月19日10:44:39 Ashish-PC SystemD[1]:FileWatcherSystemd.Service:失败,结果为“exit-code”。Jull 19 10:44:39 Ashish-PC SystemD[1]:FileWatcherSystemd.Service:服务延迟时间结束,计划重新启动。7月19日10:44:39 Ashish-PC SystemD[1]:停止的FileChangeService。Jull 19 10:44:39 Ashish-PC SystemD[1]:FileWatcherSystemd.Service:启动请求重复得太快。7月19日10:44:39 Ashish-PC SystemD[1]:启动FileChangeService失败。

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

共有1个答案

夏英发
2023-03-14

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

type=simple

您的脚本似乎没有分叉和退出。如果您的服务类型是forking,systemd假定已启动的进程forks,在后台保留运行的内容并退出。它会一直等到脚本运行完毕。

您的脚本没有退出,systemd检测到这一点并给出超时错误。你的脚本好像在无限while循环中等待键盘中断。这种脚本可以作为“简单”运行。这意味着systemd只是在后台启动脚本,并继续执行启动序列,而无需等待任何事情。

 类似资料:
  • 本文向大家介绍在Linux上学习现代服务管理系统(Systemd),包括了在Linux上学习现代服务管理系统(Systemd)的使用技巧和注意事项,需要的朋友参考一下 systemd是Linux操作系统的框架和服务管理器。在作为引导的第一个进程(如PID 1)继续运行时,它将作为init系统运行。可以提高和保持用户空间服务。本文介绍-“学习Linux上的现代服务管理系统(Systemd)” 要获取

  • 我有一个spring boot可执行罐,装在数字海洋水滴中。我可以使用现在我想让我作为一个服务运行。 我已经创建了文件/etc/systemd/system/myapp。用这些内容服务 然后使其在系统启动时启动 我正在尝试启动服务 但是我得到了这个错误 无法启动myapp.service:未知单位:myapp.service详情请参阅系统日志和'systemctl statusmyapp.serv

  • null 内存不足错误在Windows中不会发生,只有Ubuntu才会发生。在windows中,任务管理器显示100%的CPU使用率和完全的RAM使用率。我希望在Ubuntu中实现这种性能。

  • 如何为使用screen的Java应用程序设置systemd服务,并且仍然能够重新连接到该屏幕?我到处寻找答案,并尝试了几种不同的方法,但我认为systemd是在破坏整个过程。 我有一个Java应用程序设置为在system d中作为服务运行。它使用屏幕,因为我需要在它运行后能够交互式地使用它。它在Ubuntu 16.04服务器上,没有图形用户界面。我通过SSH远程管理它。Java应用程序在自己的用户

  • 问题内容: 在Ubuntu Docker映像中似乎无效或不可用。 我正在从和镜像运行Docker容器。 如果我在容器中执行,则结果为错误。在容器中,错误为。 如果我这样做,则在容器中找到systemctl,但在容器中找不到。 我发现存在。 我尝试使用安装systemd 。然后找到systemctl 但仍然给出错误 我的主要问题是:如何激活systemd和systemctl以在Ubuntu Dock

  • 问题内容: 我有一个简单的Python脚本充当守护程序。我试图创建systemd脚本,以便能够在启动过程中启动此脚本。 当前的systemd脚本: node.py: 包含循环。 我尝试使用运行该服务。不幸的是,服务从未完成说明序列-我必须按Ctrl + C。脚本正在运行,但是状态为激活,一段时间后变为停用状态。现在我正在使用(但是在我尝试不使用它之前,症状是相似的)。 我应该为脚本实现一些其他功能