从systemd运行一个持久的python脚本? [英] running a persistent python script from systemd?

查看:193
本文介绍了从systemd运行一个持久的python脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a python script that decodes the input from a usb device and sends commands to a php script. The script works beautifully when run from the console, but I need it to be run on startup.

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

I created a systemd service to start the script, which appears to work well, except that the systemctl start service-name process never returns me to the command prompt. While it is running, I can interact with the input device, exactly as expected. However, if I exit the systemctl start process with ctr-z, the script only remains running for a few seconds.

这是我编写的.service文件:

Here is the .service file that I wrote:

[Unit]
After=default.target

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

[Install]
WantedBy=default.target

这是我的python脚本:

and here is my python script:

#!/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()

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

how do I make the script run asynchronously from the service file, so that the start command can complete, and the script can continue running in the background?

推荐答案

您已经指定了After=default.targetWantedBy=default.target.这是无法解决的.

You've specified both After=default.target and WantedBy=default.target. This is not resolvable.

WantedBy指定启动时目标将包括此服务,但是After表示在启动此服务之前确保指定的目标已启动!

WantedBy specifies that the target will include this service when starting up, but After means to ensure that the named target is up before starting this service!

很可能您不需要需要After=default.target,应将其删除.

Most likely you do not need After=default.target and should remove this.

我还建议您明确指定服务Type=.尽管当前默认值为simple(这应该适合您的工作),但是较旧的systemd版本的行为可能有所不同.

I also suggest you specify the service Type= explicitly. While the default is currently simple (which should work for what you're doing) older versions of systemd may have behaved differently.

[Service]
Type=simple

这篇关于从systemd运行一个持久的python脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆