BlockchainCommons/spotbit

systemd service and automatically create a spotbit user

Fonta1n3 opened this issue · 3 comments

I do not know if I was doing something wrong but from my googling it seems you can not set up systemd services on debian for executables that are saved in the root directory.

My suggestion would be to edit the script so that it is run as root but creates a spotbit user, that way the script has full control over path variables, needs no user input and can set it all up as a systemd service without issues.

Just fyi the following worked for me to get it launching automatically after tor boots up:

nano /etc/systemd/system/spotbit.service

### paste in
-------------------------------------------
# spotbit: systemd unit
# /etc/systemd/system/spotbit.service

[Unit]
Description=spotbit
Requires=tor.service
After=tor.service

[Service]
WorkingDirectory=/home/user/.spotbit
ExecStart=/usr/local/bin/python3.8 /home/user/spotbit/server.py
User=user
Group=sudo
Type=simple

[Install]
WantedBy=multi-user.target

I have changed around the install script to install everything under a new spotbit user, that is inside of the sudo group. Now, the config file and database go to spotbit's home folder instead of root's. This removes the need to do pathlib's expanduser() call on the path provided since it can be hard coded. Hopefully this fixes the problem regarding systemd. I just tested the new install script on a debian 10 vm and it seems to still work fine with this setup.

Do you think that a lot of people would want Spotbit as a systemd service? If so, I'd love to include a service file along with the source code and just have the install script copy it to the appropriate location.

Do you think that a lot of people would want Spotbit as a systemd service? If so, I'd love to include a service file along with the source code and just have the install script copy it to the appropriate location.

I think everyone who runs it would want it as a systemd service :)

So just FYI if the install.sh is going to set up a spotbit user it should add the systemd service like so (the above was just a template it does not actually work). install.sh should also mv spotbit /home/spotbit/spotbit if people are going to git clone this as root and then install from root:

nano /etc/systemd/system/spotbit.service

### paste in
-------------------------------------------
# spotbit: systemd unit
# /etc/systemd/system/spotbit.service

[Unit]
Description=spotbit
Requires=tor.service
After=tor.service

[Service]
WorkingDirectory=/home/spotbit/.spotbit
ExecStart=/usr/local/bin/python3.8 /home/spotbit/spotbit/server.py
User=spotbit
Group=sudo
Type=simple

[Install]
WantedBy=multi-user.target

Also in order to get the above to work you need to set correct owner to the .spotbit directory:

chown spotbit /home/spotbit/.spotbit
chown spotbit /home/spotbit/.spotbit/sb.db
chown spotbit /home/spotbit/.spotbit/spotbit.log

I was getting errors about permissions to a spotbit.log that did not exist so I created it and assigned it the same owner as the .spotbit dir.

Also recommend adding usermod -aG sudo spotbit to add the spotbit user to the sudo group to avoid any issues when launching the systemd service.