SystemRage / py-kms

KMS Server Emulator written in Python
The Unlicense
2.07k stars 634 forks source link

Auto start pykms_Server.py on Raspberry Pi #54

Closed blackfuel closed 4 years ago

blackfuel commented 4 years ago

Raspberry Pi uses systemd to automatically run a program when the Pi boots. I'm getting an error about, "no display name and no $DISPLAY environment variable". However, it works fine when run from a shell prompt.

/etc/systemd/system/py-kms.service

[Unit]
Description=py-kms
After=network.target

[Service]
ExecStart=/usr/bin/python3 pykms_Server.py 0.0.0.0 1688 -V DEBUG --logfile /var/log/pykms_logserver.log
WorkingDirectory=/home/pi/py-kms/py-kms
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi

[Install]
WantedBy=multi-user.target

sudo systemctl start py-kms.service

cat /var/log/syslog

Feb 16 14:39:16 raspberrypi systemd[1]: Started py-kms.
Feb 16 14:39:17 raspberrypi python3[13828]: Traceback (most recent call last):
Feb 16 14:39:17 raspberrypi python3[13828]:   File "pykms_Server.py", line 512, in <module>
Feb 16 14:39:17 raspberrypi python3[13828]:     server_main_no_terminal()
Feb 16 14:39:17 raspberrypi python3[13828]:   File "pykms_Server.py", line 445, in server_main_no_terminal
Feb 16 14:39:17 raspberrypi python3[13828]:     server_with_gui()
Feb 16 14:39:17 raspberrypi python3[13828]:   File "pykms_Server.py", line 429, in server_with_gui
Feb 16 14:39:17 raspberrypi python3[13828]:     root = pykms_GuiBase.KmsGui()
Feb 16 14:39:17 raspberrypi python3[13828]:   File "/home/pi/py-kms/py-kms/pykms_GuiBase.py", line 69, in __init__
Feb 16 14:39:17 raspberrypi python3[13828]:     tk.Tk.__init__(self, *args, **kwargs)
Feb 16 14:39:17 raspberrypi python3[13828]:   File "/usr/lib/python3.5/tkinter/__init__.py", line 1880, in __init__
Feb 16 14:39:17 raspberrypi python3[13828]:     self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
Feb 16 14:39:17 raspberrypi python3[13828]: _tkinter.TclError: no display name and no $DISPLAY environment variable
Feb 16 14:39:17 raspberrypi python3[13828]: During handling of the above exception, another exception occurred:
Feb 16 14:39:17 raspberrypi python3[13828]: Traceback (most recent call last):
Feb 16 14:39:17 raspberrypi python3[13828]:   File "pykms_Server.py", line 514, in <module>
Feb 16 14:39:17 raspberrypi python3[13828]:     server_main_terminal()
Feb 16 14:39:17 raspberrypi python3[13828]:   File "pykms_Server.py", line 405, in server_main_terminal
Feb 16 14:39:17 raspberrypi python3[13828]:     server_check()
Feb 16 14:39:17 raspberrypi python3[13828]:   File "pykms_Server.py", line 321, in server_check
Feb 16 14:39:17 raspberrypi python3[13828]:     logger_create(loggersrv, srv_config, mode = 'a')
Feb 16 14:39:17 raspberrypi python3[13828]:   File "/home/pi/py-kms/py-kms/pykms_Misc.py", line 105, in logger_create
Feb 16 14:39:17 raspberrypi python3[13828]:     backupCount = 1, encoding = None, delay = 0))
Feb 16 14:39:17 raspberrypi python3[13828]:   File "/usr/lib/python3.5/logging/handlers.py", line 150, in __init__
Feb 16 14:39:17 raspberrypi python3[13828]:     BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
Feb 16 14:39:17 raspberrypi python3[13828]:   File "/usr/lib/python3.5/logging/handlers.py", line 57, in __init__
Feb 16 14:39:17 raspberrypi python3[13828]:     logging.FileHandler.__init__(self, filename, mode, encoding, delay)
Feb 16 14:39:17 raspberrypi python3[13828]:   File "/usr/lib/python3.5/logging/__init__.py", line 1009, in __init__
Feb 16 14:39:17 raspberrypi python3[13828]:     StreamHandler.__init__(self, self._open())
Feb 16 14:39:17 raspberrypi python3[13828]:   File "/usr/lib/python3.5/logging/__init__.py", line 1038, in _open
Feb 16 14:39:17 raspberrypi python3[13828]:     return open(self.baseFilename, self.mode, encoding=self.encoding)
Feb 16 14:39:17 raspberrypi python3[13828]: PermissionError: [Errno 13] Permission denied: '/var/log/pykms_logserver.log'
Feb 16 14:39:17 raspberrypi systemd[1]: py-kms.service: Main process exited, code=exited, status=1/FAILURE
Feb 16 14:39:17 raspberrypi systemd[1]: py-kms.service: Unit entered failed state.
Feb 16 14:39:17 raspberrypi systemd[1]: py-kms.service: Failed with result 'exit-code'.
Feb 16 14:39:18 raspberrypi systemd[1]: py-kms.service: Service hold-off time over, scheduling restart.
Feb 16 14:39:18 raspberrypi systemd[1]: Stopped py-kms.
Feb 16 14:39:18 raspberrypi systemd[1]: Started py-kms.
Feb 16 14:39:19 raspberrypi python3[13836]: Traceback (most recent call last):
Feb 16 14:39:19 raspberrypi python3[13836]:   File "pykms_Server.py", line 512, in <module>
Feb 16 14:39:19 raspberrypi python3[13836]:     server_main_no_terminal()
Feb 16 14:39:19 raspberrypi python3[13836]:   File "pykms_Server.py", line 445, in server_main_no_terminal
Feb 16 14:39:19 raspberrypi python3[13836]:     server_with_gui()
Feb 16 14:39:19 raspberrypi python3[13836]:   File "pykms_Server.py", line 429, in server_with_gui
Feb 16 14:39:19 raspberrypi python3[13836]:     root = pykms_GuiBase.KmsGui()
Feb 16 14:39:19 raspberrypi python3[13836]:   File "/home/pi/py-kms/py-kms/pykms_GuiBase.py", line 69, in __init__
Feb 16 14:39:19 raspberrypi python3[13836]:     tk.Tk.__init__(self, *args, **kwargs)
Feb 16 14:39:19 raspberrypi python3[13836]:   File "/usr/lib/python3.5/tkinter/__init__.py", line 1880, in __init__
Feb 16 14:39:19 raspberrypi python3[13836]:     self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
Feb 16 14:39:19 raspberrypi python3[13836]: _tkinter.TclError: no display name and no $DISPLAY environment variable
Feb 16 14:39:19 raspberrypi python3[13836]: During handling of the above exception, another exception occurred:
Feb 16 14:39:19 raspberrypi python3[13836]: Traceback (most recent call last):
Feb 16 14:39:19 raspberrypi python3[13836]:   File "pykms_Server.py", line 514, in <module>
Feb 16 14:39:19 raspberrypi python3[13836]:     server_main_terminal()
Feb 16 14:39:19 raspberrypi python3[13836]:   File "pykms_Server.py", line 405, in server_main_terminal
Feb 16 14:39:19 raspberrypi python3[13836]:     server_check()
Feb 16 14:39:19 raspberrypi python3[13836]:   File "pykms_Server.py", line 321, in server_check
Feb 16 14:39:19 raspberrypi python3[13836]:     logger_create(loggersrv, srv_config, mode = 'a')
Feb 16 14:39:19 raspberrypi python3[13836]:   File "/home/pi/py-kms/py-kms/pykms_Misc.py", line 105, in logger_create
Feb 16 14:39:19 raspberrypi python3[13836]:     backupCount = 1, encoding = None, delay = 0))
Feb 16 14:39:19 raspberrypi python3[13836]:   File "/usr/lib/python3.5/logging/handlers.py", line 150, in __init__
Feb 16 14:39:19 raspberrypi python3[13836]:     BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
Feb 16 14:39:19 raspberrypi python3[13836]:   File "/usr/lib/python3.5/logging/handlers.py", line 57, in __init__
Feb 16 14:39:19 raspberrypi python3[13836]:     logging.FileHandler.__init__(self, filename, mode, encoding, delay)
Feb 16 14:39:19 raspberrypi python3[13836]:   File "/usr/lib/python3.5/logging/__init__.py", line 1009, in __init__
Feb 16 14:39:19 raspberrypi python3[13836]:     StreamHandler.__init__(self, self._open())
Feb 16 14:39:19 raspberrypi python3[13836]:   File "/usr/lib/python3.5/logging/__init__.py", line 1038, in _open
Feb 16 14:39:19 raspberrypi python3[13836]:     return open(self.baseFilename, self.mode, encoding=self.encoding)
Feb 16 14:39:19 raspberrypi python3[13836]: PermissionError: [Errno 13] Permission denied: '/var/log/pykms_logserver.log'
Feb 16 14:39:19 raspberrypi systemd[1]: py-kms.service: Main process exited, code=exited, status=1/FAILURE
Feb 16 14:39:19 raspberrypi systemd[1]: py-kms.service: Unit entered failed state.
Feb 16 14:39:19 raspberrypi systemd[1]: py-kms.service: Failed with result 'exit-code'.
Feb 16 14:39:19 raspberrypi systemd[1]: py-kms.service: Service hold-off time over, scheduling restart.
Feb 16 14:39:19 raspberrypi systemd[1]: Stopped py-kms.
Feb 16 14:39:19 raspberrypi systemd[1]: Started py-kms.
Feb 16 14:39:20 raspberrypi python3[13841]: Traceback (most recent call last):
Feb 16 14:39:20 raspberrypi python3[13841]:   File "pykms_Server.py", line 512, in <module>
Feb 16 14:39:20 raspberrypi python3[13841]:     server_main_no_terminal()
Feb 16 14:39:20 raspberrypi python3[13841]:   File "pykms_Server.py", line 445, in server_main_no_terminal
Feb 16 14:39:20 raspberrypi python3[13841]:     server_with_gui()
Feb 16 14:39:20 raspberrypi python3[13841]:   File "pykms_Server.py", line 429, in server_with_gui
Feb 16 14:39:20 raspberrypi python3[13841]:     root = pykms_GuiBase.KmsGui()
Feb 16 14:39:20 raspberrypi python3[13841]:   File "/home/pi/py-kms/py-kms/pykms_GuiBase.py", line 69, in __init__
Feb 16 14:39:20 raspberrypi python3[13841]:     tk.Tk.__init__(self, *args, **kwargs)
Feb 16 14:39:20 raspberrypi python3[13841]:   File "/usr/lib/python3.5/tkinter/__init__.py", line 1880, in __init__
Feb 16 14:39:20 raspberrypi python3[13841]:     self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
Feb 16 14:39:20 raspberrypi python3[13841]: _tkinter.TclError: no display name and no $DISPLAY environment variable
Feb 16 14:39:20 raspberrypi python3[13841]: During handling of the above exception, another exception occurred:
Feb 16 14:39:20 raspberrypi python3[13841]: Traceback (most recent call last):
Feb 16 14:39:20 raspberrypi python3[13841]:   File "pykms_Server.py", line 514, in <module>
Feb 16 14:39:20 raspberrypi python3[13841]:     server_main_terminal()
Feb 16 14:39:20 raspberrypi python3[13841]:   File "pykms_Server.py", line 405, in server_main_terminal
Feb 16 14:39:20 raspberrypi python3[13841]:     server_check()
Feb 16 14:39:20 raspberrypi python3[13841]:   File "pykms_Server.py", line 321, in server_check
Feb 16 14:39:20 raspberrypi python3[13841]:     logger_create(loggersrv, srv_config, mode = 'a')
Feb 16 14:39:20 raspberrypi python3[13841]:   File "/home/pi/py-kms/py-kms/pykms_Misc.py", line 105, in logger_create
Feb 16 14:39:20 raspberrypi python3[13841]:     backupCount = 1, encoding = None, delay = 0))
Feb 16 14:39:20 raspberrypi python3[13841]:   File "/usr/lib/python3.5/logging/handlers.py", line 150, in __init__
Feb 16 14:39:20 raspberrypi python3[13841]:     BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
Feb 16 14:39:20 raspberrypi python3[13841]:   File "/usr/lib/python3.5/logging/handlers.py", line 57, in __init__
Feb 16 14:39:20 raspberrypi python3[13841]:     logging.FileHandler.__init__(self, filename, mode, encoding, delay)
Feb 16 14:39:20 raspberrypi python3[13841]:   File "/usr/lib/python3.5/logging/__init__.py", line 1009, in __init__
Feb 16 14:39:20 raspberrypi python3[13841]:     StreamHandler.__init__(self, self._open())
Feb 16 14:39:20 raspberrypi python3[13841]:   File "/usr/lib/python3.5/logging/__init__.py", line 1038, in _open
Feb 16 14:39:20 raspberrypi python3[13841]:     return open(self.baseFilename, self.mode, encoding=self.encoding)
Feb 16 14:39:20 raspberrypi python3[13841]: PermissionError: [Errno 13] Permission denied: '/var/log/pykms_logserver.log'
Feb 16 14:39:20 raspberrypi systemd[1]: py-kms.service: Main process exited, code=exited, status=1/FAILURE
Feb 16 14:39:20 raspberrypi systemd[1]: py-kms.service: Unit entered failed state.
Feb 16 14:39:20 raspberrypi systemd[1]: py-kms.service: Failed with result 'exit-code'.
Feb 16 14:39:20 raspberrypi systemd[1]: py-kms.service: Service hold-off time over, scheduling restart.
Feb 16 14:39:20 raspberrypi systemd[1]: Stopped py-kms.
Feb 16 14:39:20 raspberrypi systemd[1]: Started py-kms.
Feb 16 14:39:21 raspberrypi python3[13847]: Traceback (most recent call last):
Feb 16 14:39:21 raspberrypi python3[13847]:   File "pykms_Server.py", line 512, in <module>
Feb 16 14:39:21 raspberrypi python3[13847]:     server_main_no_terminal()
Feb 16 14:39:21 raspberrypi python3[13847]:   File "pykms_Server.py", line 445, in server_main_no_terminal
Feb 16 14:39:21 raspberrypi python3[13847]:     server_with_gui()
Feb 16 14:39:21 raspberrypi python3[13847]:   File "pykms_Server.py", line 429, in server_with_gui
Feb 16 14:39:21 raspberrypi python3[13847]:     root = pykms_GuiBase.KmsGui()
Feb 16 14:39:21 raspberrypi python3[13847]:   File "/home/pi/py-kms/py-kms/pykms_GuiBase.py", line 69, in __init__
Feb 16 14:39:21 raspberrypi python3[13847]:     tk.Tk.__init__(self, *args, **kwargs)
Feb 16 14:39:21 raspberrypi python3[13847]:   File "/usr/lib/python3.5/tkinter/__init__.py", line 1880, in __init__
Feb 16 14:39:21 raspberrypi python3[13847]:     self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
Feb 16 14:39:21 raspberrypi python3[13847]: _tkinter.TclError: no display name and no $DISPLAY environment variable
Feb 16 14:39:21 raspberrypi python3[13847]: During handling of the above exception, another exception occurred:
Feb 16 14:39:21 raspberrypi python3[13847]: Traceback (most recent call last):
Feb 16 14:39:21 raspberrypi python3[13847]:   File "pykms_Server.py", line 514, in <module>
Feb 16 14:39:21 raspberrypi python3[13847]:     server_main_terminal()
Feb 16 14:39:21 raspberrypi python3[13847]:   File "pykms_Server.py", line 405, in server_main_terminal
Feb 16 14:39:21 raspberrypi python3[13847]:     server_check()
Feb 16 14:39:21 raspberrypi python3[13847]:   File "pykms_Server.py", line 321, in server_check
Feb 16 14:39:21 raspberrypi python3[13847]:     logger_create(loggersrv, srv_config, mode = 'a')
Feb 16 14:39:21 raspberrypi python3[13847]:   File "/home/pi/py-kms/py-kms/pykms_Misc.py", line 105, in logger_create
Feb 16 14:39:21 raspberrypi python3[13847]:     backupCount = 1, encoding = None, delay = 0))
Feb 16 14:39:21 raspberrypi python3[13847]:   File "/usr/lib/python3.5/logging/handlers.py", line 150, in __init__
Feb 16 14:39:21 raspberrypi python3[13847]:     BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
Feb 16 14:39:21 raspberrypi python3[13847]:   File "/usr/lib/python3.5/logging/handlers.py", line 57, in __init__
Feb 16 14:39:21 raspberrypi python3[13847]:     logging.FileHandler.__init__(self, filename, mode, encoding, delay)
Feb 16 14:39:21 raspberrypi python3[13847]:   File "/usr/lib/python3.5/logging/__init__.py", line 1009, in __init__
Feb 16 14:39:21 raspberrypi python3[13847]:     StreamHandler.__init__(self, self._open())
Feb 16 14:39:21 raspberrypi python3[13847]:   File "/usr/lib/python3.5/logging/__init__.py", line 1038, in _open
Feb 16 14:39:21 raspberrypi python3[13847]:     return open(self.baseFilename, self.mode, encoding=self.encoding)
Feb 16 14:39:21 raspberrypi python3[13847]: PermissionError: [Errno 13] Permission denied: '/var/log/pykms_logserver.log'
Feb 16 14:39:21 raspberrypi systemd[1]: py-kms.service: Main process exited, code=exited, status=1/FAILURE
Feb 16 14:39:21 raspberrypi systemd[1]: py-kms.service: Unit entered failed state.
Feb 16 14:39:21 raspberrypi systemd[1]: py-kms.service: Failed with result 'exit-code'.
Feb 16 14:39:21 raspberrypi systemd[1]: py-kms.service: Service hold-off time over, scheduling restart.
Feb 16 14:39:21 raspberrypi systemd[1]: Stopped py-kms.
Feb 16 14:39:21 raspberrypi systemd[1]: Started py-kms.
Feb 16 14:39:22 raspberrypi python3[13852]: Traceback (most recent call last):
Feb 16 14:39:22 raspberrypi python3[13852]:   File "pykms_Server.py", line 512, in <module>
Feb 16 14:39:22 raspberrypi python3[13852]:     server_main_no_terminal()
Feb 16 14:39:22 raspberrypi python3[13852]:   File "pykms_Server.py", line 445, in server_main_no_terminal
Feb 16 14:39:22 raspberrypi python3[13852]:     server_with_gui()
Feb 16 14:39:22 raspberrypi python3[13852]:   File "pykms_Server.py", line 429, in server_with_gui
Feb 16 14:39:22 raspberrypi python3[13852]:     root = pykms_GuiBase.KmsGui()
Feb 16 14:39:22 raspberrypi python3[13852]:   File "/home/pi/py-kms/py-kms/pykms_GuiBase.py", line 69, in __init__
Feb 16 14:39:22 raspberrypi python3[13852]:     tk.Tk.__init__(self, *args, **kwargs)
Feb 16 14:39:22 raspberrypi python3[13852]:   File "/usr/lib/python3.5/tkinter/__init__.py", line 1880, in __init__
Feb 16 14:39:22 raspberrypi python3[13852]:     self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
Feb 16 14:39:22 raspberrypi python3[13852]: _tkinter.TclError: no display name and no $DISPLAY environment variable
Feb 16 14:39:22 raspberrypi python3[13852]: During handling of the above exception, another exception occurred:
Feb 16 14:39:22 raspberrypi python3[13852]: Traceback (most recent call last):
Feb 16 14:39:22 raspberrypi python3[13852]:   File "pykms_Server.py", line 514, in <module>
Feb 16 14:39:22 raspberrypi python3[13852]:     server_main_terminal()
Feb 16 14:39:22 raspberrypi python3[13852]:   File "pykms_Server.py", line 405, in server_main_terminal
Feb 16 14:39:22 raspberrypi python3[13852]:     server_check()
Feb 16 14:39:22 raspberrypi python3[13852]:   File "pykms_Server.py", line 321, in server_check
Feb 16 14:39:22 raspberrypi python3[13852]:     logger_create(loggersrv, srv_config, mode = 'a')
Feb 16 14:39:22 raspberrypi python3[13852]:   File "/home/pi/py-kms/py-kms/pykms_Misc.py", line 105, in logger_create
Feb 16 14:39:22 raspberrypi python3[13852]:     backupCount = 1, encoding = None, delay = 0))
Feb 16 14:39:22 raspberrypi python3[13852]:   File "/usr/lib/python3.5/logging/handlers.py", line 150, in __init__
Feb 16 14:39:22 raspberrypi python3[13852]:     BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
Feb 16 14:39:22 raspberrypi python3[13852]:   File "/usr/lib/python3.5/logging/handlers.py", line 57, in __init__
Feb 16 14:39:22 raspberrypi python3[13852]:     logging.FileHandler.__init__(self, filename, mode, encoding, delay)
Feb 16 14:39:22 raspberrypi python3[13852]:   File "/usr/lib/python3.5/logging/__init__.py", line 1009, in __init__
Feb 16 14:39:22 raspberrypi python3[13852]:     StreamHandler.__init__(self, self._open())
Feb 16 14:39:22 raspberrypi python3[13852]:   File "/usr/lib/python3.5/logging/__init__.py", line 1038, in _open
Feb 16 14:39:22 raspberrypi python3[13852]:     return open(self.baseFilename, self.mode, encoding=self.encoding)
Feb 16 14:39:22 raspberrypi python3[13852]: PermissionError: [Errno 13] Permission denied: '/var/log/pykms_logserver.log'
Feb 16 14:39:22 raspberrypi systemd[1]: py-kms.service: Main process exited, code=exited, status=1/FAILURE
Feb 16 14:39:22 raspberrypi systemd[1]: py-kms.service: Unit entered failed state.
Feb 16 14:39:22 raspberrypi systemd[1]: py-kms.service: Failed with result 'exit-code'.
Feb 16 14:39:23 raspberrypi systemd[1]: py-kms.service: Service hold-off time over, scheduling restart.
Feb 16 14:39:23 raspberrypi systemd[1]: Stopped py-kms.
Feb 16 14:39:23 raspberrypi systemd[1]: py-kms.service: Start request repeated too quickly.
Feb 16 14:39:23 raspberrypi systemd[1]: Failed to start py-kms.
Feb 16 14:39:23 raspberrypi systemd[1]: py-kms.service: Unit entered failed state.
Feb 16 14:39:23 raspberrypi systemd[1]: py-kms.service: Failed with result 'exit-code'.
blackfuel commented 4 years ago

I managed to get it working by running as root. Here's my systemd service for py-kms on Raspberry Pi.

/etc/systemd/system/py-kms.service

[Unit]
Description=py-kms
After=network.target

[Service]
ExecStartPre=/bin/sleep 2
ExecStart=/usr/bin/python3 -u pykms_Server.py 0.0.0.0 1688 --loglevel ERROR --logfile /var/log/pykms_logserver.log
WorkingDirectory=/home/pi/py-kms/py-kms
Restart=always

[Install]
WantedBy=multi-user.target

Commands to control systemd service on Raspberry Pi

sudo systemctl start py-kms.service
sudo systemctl stop py-kms.service
sudo systemctl enable py-kms.service
sudo systemctl disable py-kms.service