【问题标题】:running a persistent python script from systemd?从systemd运行持久的python脚本?
【发布时间】:2013-08-07 20:32:57
【问题描述】:

我有一个 python 脚本,它可以解码来自 USB 设备的输入并将命令发送到 php 脚本。该脚本从控制台运行时运行良好,但我需要它在启动时运行。

我创建了一个 systemd 服务来启动脚本,它似乎运行良好,除了 systemctl start service-name 进程永远不会让我返回到命令提示符。在它运行时,我可以完全按照预期与输入设备进行交互。但是,如果我用 ctr-z 退出 systemctl start 进程,脚本只会运行几秒钟。

这是我写的 .service 文件:

[Unit]
After=default.target

[Service]
ExecStart=/usr/bin/python /root/pidora-keyboard.py

[Install]
WantedBy=default.target

这是我的python脚本:

#!/usr/bin/env python
import json, random
from evdev import InputDevice, categorize, ecodes
from urllib.request import urlopen

dev = InputDevice('/dev/input/event2')

def sendCommand(c):
    return json.loads(urlopen("http://127.0.0.1/api.php?command="+c).read().decode("utf-8"))
def getRandomStation():
    list = sendCommand('stationList')
    list = list['stations']
    index = random.randint(0, (len(list)-1))
    print(list[index]['id'] + " - " + list[index]['name'])
    sendCommand('s' + list[index]['id'])

print(dev)
for event in dev.read_loop():
    if event.type == ecodes.EV_KEY:
        key_pressed = str(categorize(event))
        if ', down' in key_pressed:
            print(key_pressed)
            if 'KEY_PLAYPAUSE' in key_pressed:
                print('play')
                sendCommand('p')
            if 'KEY_FASTFORWARD' in key_pressed:
                print('fastforward')
                sendCommand('n')
            if 'KEY_NEXTSONG' in key_pressed:
                print('skip')
                sendCommand('n')
            if 'KEY_POWER' in key_pressed:
                print('power')
                sendCommand('q')
            if 'KEY_VOLUMEUP' in key_pressed:
                print('volume up')
                sendCommand('v%2b')
            if 'KEY_VOLUMEDOWN' in key_pressed:
                print('volume down')
                sendCommand('v-')
            if 'KEY_CONFIG' in key_pressed:
                print('Random Station')
                getRandomStation()

如何让脚本从服务文件中异步运行,让启动命令完成,脚本可以在后台继续运行?

【问题讨论】:

  • 如果在ExecStart 行的末尾添加& 会怎样?这不会强制脚本在后台运行吗? For example.
  • @Bill 刚刚尝试了ExecStart=/usr/bin/python /root/pidora-keyboard.py & > /dev/null & 的极端版本​​,但它仍然挂起,如果我杀死它,子进程会在几秒钟后被杀死。
  • [Service] 下的Type=forking 怎么样?如果你这样做,你可能也应该删除&。
  • 您的(编辑的)代码和单元文件在我的 systemd 上运行良好。作为旁注,我可以建议pyzmo,这是一个基于 python-evdev 构建的小型热键库。
  • @ bill,他应该叉子,然后使用适当的选项集创建PID文件。使用this,Greg Schoppe。

标签: python linux bash systemd


【解决方案1】:

您同时指定了After=default.target 和WantedBy=default.target。这是无法解决的。

WantedBy 指定目标在启动时会包含这个服务,但 After 表示在启动这个服务之前确保命名的目标已经启动!

您很可能不需要需要After=default.target,应该删除它。


我还建议您明确指定服务Type=。虽然当前默认值为 simple(这应该适用于您正在做的事情),但旧版本的 systemd 的行为可能有所不同。

[Service]
Type=simple

【讨论】:

    【解决方案2】:

    usig nohup 呢? http://en.wikipedia.org/wiki/Nohup nohup 是一个忽略 HUP(挂断)信号的 POSIX 命令。 HUP(挂断)信号按照惯例是终端警告相关进程注销的方式。

    【讨论】:

      猜你喜欢
      • 2021-02-13
      • 2015-01-06
      • 1970-01-01
      • 1970-01-01
      • 2017-08-01
      • 2022-12-07
      • 1970-01-01
      • 2020-10-11
      • 1970-01-01
      相关资源
      最近更新 更多