SystemRage / py-kms

KMS Server Emulator written in Python
The Unlicense
2.05k stars 628 forks source link

How to run at startup/as service? #4

Closed ricktendo closed 5 years ago

ricktendo commented 5 years ago

Hi,

Having problems starting as a service

ricktendo commented 5 years ago

Here is my /etc/init/py-kms.conf

description "py-kms"
author "myanaloglife"
env PYTHON_HOME=/usr/bin
env PATH=$PYTHON_HOME:$PATH
start on runlevel [2345]
stop on runlevel [016]
chdir /opt/py3-kms
exec $PYTHON_HOME/python3 server.py 0.0.0.0 -v DEBUG --logfile /var/log/py-kms_server.log
respawn

Also the following commands do not work on the Raspberry Pi (maybe systemctl instead?)

init-checkconf /etc/init/py-kms.conf
initctl reload-configuration
ricktendo commented 5 years ago

Update: So this seems to have done the trick, if you see anything that needs tweaking please let me know

nano /lib/systemd/system/py3-kms.service

py3-kms.service file code

[Unit]
Description=py3-kms
After=network-online.target
Wants=network-online.target

[Service]
Restart=always
Type=simple
ExecStart=/usr/bin/python3 /opt/py3-kms/server.py -v DEBUG --logfile /var/log/py-kms_server.log

[Install]
WantedBy=multi-user.target

systemctl enable py3-kms

SystemRage commented 5 years ago

You could also try:

[Unit]
Description=py3-kms
After=network.target
Wants=network-online.target

[Service]
ExecStart=/usr/bin/python3 /opt/py3-kms/server.py -v DEBUG --logfile /var/log/py-kms_server.log
Restart=always
KillMode=process
Type=idle
User=nobody
Group=nogroup

[Install]
WantedBy=multi-user.target

Then: sudo chmod 644 /lib/systemd/system/py3-kms.service sudo systemctl daemon-reload sudo systemctl enable py3-kms.service

mobisat commented 5 years ago

If I use the code by @SystemRage on October 5th will the KMS server run whenever the Raspberry Pi is turned on? Do I need to use the code by @ricktendo as well, (/etc/init/py-kms.conf)?

tlc76 commented 5 years ago

Hi, I have upgraded py-kms to the latest version on my Raspberry Pi 3.

However the service cannot start anymore due to the following error:

May 30 15:58:16 pi systemd[1]: Started Python KMS Server.
May 30 15:58:16 pi python[28247]: Traceback (most recent call last):
May 30 15:58:16 pi python[28247]:   File "/opt/py-kms/py-kms/pykms_Server.py", line 266, in <module>
May 30 15:58:16 pi python[28247]:     srv_main_with_gui()
May 30 15:58:16 pi python[28247]:   File "/opt/py-kms/py-kms/pykms_Server.py", line 197, in srv_main_with_gui
May 30 15:58:16 pi python[28247]:     root = pykms_GuiBase.KmsGui()
May 30 15:58:16 pi python[28247]:   File "/opt/py-kms/py-kms/pykms_GuiBase.py", line 77, in __init__
May 30 15:58:16 pi python[28247]:     tk.Tk.__init__(self, *args, **kwargs)
May 30 15:58:16 pi python[28247]:   File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1823, in __init__
May 30 15:58:16 pi python[28247]:     self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
May 30 15:58:16 pi python[28247]: _tkinter.TclError: no display name and no $DISPLAY environment variable
May 30 15:58:16 pi systemd[1]: py-kms.service: Main process exited, code=exited, status=1/FAILURE
May 30 15:58:16 pi systemd[1]: py-kms.service: Unit entered failed state.
May 30 15:58:16 pi systemd[1]: py-kms.service: Failed with result 'exit-code'.

It looks like it is trying to start the GUI.

The service config file /etc/systemd/system/py-kms.service contains the following:

[Unit]
Description=Python KMS Server

[Service]
ExecStart=/usr/bin/python /opt/py-kms/py-kms/pykms_Server.py
User=openhabian
Type=simple
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

(I have also python3 but I get the same error if I change the ExecStart to /usr/bin/python3).

However, if I manually run the ExecStart command from a shell prompt, the server starts in interactive mode without any problem!

Can you please advise how to proceed?

Thanks!

SystemRage commented 5 years ago

@mobisat you have to choose if you want to use upstart (/etc/init/py-kms.conf) or systemd (/lib/systemd/system/py-kms.service) i can't test which really works because i don't have a Raspberry Pi. @tlc76 https://github.com/SystemRage/py-kms/issues/34#issuecomment-498438644

tedaz commented 4 years ago

Below scripts works great on Ubuntu 16.04 with systemctl.

enter the folder: cd /usr/local/

clone the code: git clone https://github.com/SystemRage/py-kms.git

create the service script: nano /etc/systemd/system/py3-kms.service

[Unit] Description=py3-kms After=network.target StartLimitIntervalSec=0

[Service] Type=simple Restart=always RestartSec=1 User=root ExecStart=/usr/bin/python3 /usr/local/py-kms/py-kms/pykms_Server.py

[Install] WantedBy=multi-user.target

remarks: do not use the paramater "-v" in the service script.

enable, start and check status of the script: systemctl enable py3-kms

systemctl start py3-kms

systemctl restart py3-kms

systemctl status py3-kms

After any revision of the service script, please run systemctl daemon-reload before restart the service.

systemctl status py3-kms ● py3-kms.service - py3-kms Loaded: loaded (/etc/systemd/system/py3-kms.service; enabled; vendor preset: enabled) Active: active (running) since Sun 2020-04-12 06:45:14 PDT; 2s ago Main PID: 27885 (python3) Tasks: 3 Memory: 11.5M CPU: 145ms CGroup: /system.slice/py3-kms.service └─27885 /usr/bin/python3 /usr/local/py-kms/py-kms/pykms_Server.py

simonmicro commented 4 years ago

Hey, you could also use the new docker image option. Take a look into the docker folder for that. It includes also a restart at boot time.