mjvotaw / plex-sub-downloader

An attempt to build a simple command-line tool for automating the adding of subtitles to Plex content.
MIT License
13 stars 3 forks source link

Run as a service (Linux) #14

Open Varming73 opened 10 months ago

Varming73 commented 10 months ago

Maybe a n00b question but how do I make this run as a service on Linux?

Even better would be as a docker container but maybe it's too bit of an ask (I have no idea how complicated that might be)

mjvotaw commented 10 months ago

Sure, I can try to give you a run-down of how to set up a basic systemd service.

First, here's the contents of my plex_sub_downloader.service file (located at /etc/systemd/system/plex_sub_downloader.service):

[Unit]
Description=Plex Sub Downloader
After=plexmediaserver.service

[Service]
SyslogIdentifier=plex_sub_downloader
Restart=always
RestartSec=5
Type=simple
User=plex
Group=video
WorkingDirectory=/var/opt/plex_sub_downloader
ExecStart=/usr/local/bin/plex_sub_downloader --config /var/opt/plex_sub_downloader/config.json start-webhook
TimeoutStopSec=30

[Install]
WantedBy=multi-user.target

Things to note: After: plexmediaserver.service is only relevant if you're also running Plex as a service. If you're running it through docker, you'll want to change that line to After: docker.service (assuming you're running docker as a service, of course).

User and Group are the user and group that the service runs as. You want to set these to a user/group that has permissions to read your config file. If you're using the with_media option for subtitle_destination, then the user/group will also need access to your Plex content.

I keep my config file located at /var/opt/plex_sub_downloader/config.json, but you can store it wherever you want. I keep a lot of my config files at /var/opt just so I know where they're at.

WorkingDirectory sets the directory that the service executes from. This is probably not actually necessary. ExecStart is the command that is run when the service starts. Notice that I have the full file paths to plex_sub_downloader and config.json. You can get the full path by which plex_sub_downloader.

And how to set it all up: So you take all of that and stick in a file at /etc/systemd/system/plex_sub_downloader.service (you'll need root access to write that file).

Then, tell systemd to reload the contents of that directory using sudo systemctl daemon-reload Then, enable the service by sudo systemctl enable plex_sub_downloader.service. "Enabling" it tells systemd to start this service whenever the computer starts up. And finally, you can start the service now by sudo systemctl start plex_sub_downloader.service, and check the status by sudo systemctl status plex_sub_downloader.service

mjvotaw commented 10 months ago

And as for a docker image, I don't have much experience setting one up for public distribution, so it's never been something I've really thought to do. I'm certainly open to it, as I know a lot of people already use it for things like Plex.

Varming73 commented 10 months ago

Thanks a lot! This is more or less what I've already tried. I guess the real problem is that I'm trying to do this on the one Linux server I already got running which has Conda installed which seems to mess with this as it has it's own Python environment.

Besides this one server (and Home Assistant on it's own Linux fork) everything is running in containers on Unraid on my server. It would be perfect to have this script doing it too as it then wouldn't be necessary to create a Linux server to run one script. I've never created a docker container from scratch but I guess you could take a Python container and modify it for the purpose.

UPDATE: I just installed a clean Linux server and setting up the service there runs as expected.

Varming73 commented 10 months ago

I just found an article on how to create the docker version: https://medium.com/vantageai/how-to-make-your-python-docker-images-secure-fast-small-b3a6870373a0

Your requirements file seems to set up a lot of stuff (which I'm sure why you named it -dev) so I'm not sure what is needed in the container and what is not.

mjvotaw commented 10 months ago

Thanks, I'll take a look at that article. And as for dependencies, you can find that in pyproject.toml. The requirements-dev.txt is just that--an exhaustive list of all the packages needed when building the package. Most of that list is just dependencies from other packages.