私は、USBデバイスからの入力をデコードし、コマンドをPHPスクリプトに送るpythonスクリプトを持っています。このスクリプトはコンソールから実行するときれいに動作しますが、起動時に実行する必要があります。システムファイルから永続的なpythonスクリプトを実行していますか?
スクリプトを起動するためのsystemdサービスを作成しましたが、これは正常に動作しますが、systemctl start service-name
プロセスが私をコマンドプロンプトに返すことはありません。実行中は、まったく同じように入力デバイスと対話できます。しかし、systemctl start
プロセスをctr-zで終了すると、スクリプトは数秒間だけ実行されます。ここで
は、私が書いた.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'行の最後に'& 'を追加した場合はどうなりますか?スクリプトを強制的にバックグラウンドで実行するのではないでしょうか? [例](http://bashitout.com/2013/05/18/Ampersands-on-the-command-line.html)。 – wflynny
@Billはその 'ExecStart =/usr/bin/python /root/pidora-keyboard.py>/dev/null&'の極端なバージョンを試しましたが、まだハングアップしています。数秒後に殺された。 –
'[Service]'の 'Type = forking'はどうですか?これを行うと、おそらく '&'も削除するべきです。 – wflynny