title: running a persistent python script from systemd?
body: <p>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.</p>
  
  <p>I created a systemd service to start the script, which appears to work well, except that the <code>systemctl start service-name</code> 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 <code>systemctl start</code> process with ctr-z, the script only remains running for a few seconds.</p>
  
  <p>Here is the .service file that I wrote:</p>
  
  <pre><code>[Unit]
  After=default.target
  
  [Service]
  ExecStart=/usr/bin/python /root/pidora-keyboard.py
  
  [Install]
  WantedBy=default.target
  </code></pre>
  
  <p>and here is my python script:</p>
  
  <pre><code>#!/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()
  </code></pre>
  
  <p>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?</p>