मेरे पास एक पायथन स्क्रिप्ट है जो यूएसबी डिवाइस से इनपुट को डीकोड करता है और कमांड को 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
और यहां मेरे अजगर स्क्रिप्ट है:
#!/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' लाइन के अंत में' & 'जोड़ा है? क्या वह पृष्ठभूमि में स्क्रिप्ट को चलाने के लिए मजबूर नहीं करेगा? [उदाहरण के लिए] (http://bashitout.com/2013/05/18/Ampersands-on-the-command-line.html)। – wflynny
@ बिल ने अभी तक 'ExecStart =/usr/bin/python /root/pidora-keyboard.py &>/dev/null & 'के चरम संस्करण को आजमाया है, और यह अभी भी लटकता है, और यदि मैं इसे मारता हूं, तो सबप्रोसेस हो जाता है कुछ सेकंड बाद मारे गए। –
'[सेवा]' के तहत 'टाइप = फोर्किंग'' के बारे में क्या? यदि आप ऐसा करते हैं, तो आपको शायद '&' को भी हटा देना चाहिए। – wflynny