jellyfin / jellyfin-mpv-shim

MPV Cast Client for Jellyfin
Other
1.5k stars 88 forks source link

Systemd service to start jellyfin-mpv-shim #330

Open oschwand opened 1 year ago

oschwand commented 1 year ago

Here is a proposal for a systemd unit to manage the jellyfin-mpv-shim. I use it on a media player without keyboard access. It may be of interest for #174.

It's far from perfect:

I don't know how to deal properly with the first issue, but it's not really a problem.

For second one, my workaround is the following. I add a condition in order to check if my Jellyfin server is accessible

ExecCondition=bash -c "curl --fail http://jellyfin || exit 255"; echo $?

(the bashism is used to get a proper exit code for systemd). But unfortunately the URL is hard-coded...

A proper way to fix this issue would be to have a command line argument which exits the shim when the connection fails, instead of asking the user to create a new one. I will try to work on this point, but the command line processing is rather primitive for now.

iwalton3 commented 1 year ago

There is a config option to wait for a period of time before connecting. A command option to make it wait and then fail if it can't connect makes sense.

oschwand commented 1 year ago

Indeed, connect_retry_mins is useful for this usecase.

bgstack15 commented 1 year ago

Users can always use a systemctl edit --user jellyfin-mpv-shim.service and basically rewrite the entire ExecCondition, or perhaps you could experiment to see if the ExecCondition parses $ENVVARIABLE used like so in the .service file:

Environment=JELLYFIN_URL=http://jellyfin:8096
ExecCondition=bash -c "curl --fail ${JELLYFIN_URL} || exit 255"; echo $?

Environment is demonstrated in the man page with ExecStart, but not ExecCondition so this should be tested. And then if it works, the user can still use systemctl edit --user jellyfin-mpv-shim.service to then just add the one entry for Environment=JELLYFIN_URL=http://jellyfin:8096 and not the whole logic.

gudvinr commented 3 months ago

it assumes an installation in ~/.local/bin, which should be the case when installed with pip{x},

Not everyone uses PIP. You shouldn't assume this for unit file that could be distributed to broad audience.

Hardcoding paths is acceptable if you are both building and packaging software but for other cases just use executable name. $PATH should do the rest.

It's also worth adding

After=network-online.target

If you run jellyfin server on the same PC, it won't do anything basically, but if your server is on the network, there is no point in starting shim before network is up. ExecCondition in this case will fail too.

For exec condition, to avoid hardcoding URL something like this can be used:

cat "${XDG_CONFIG_HOME:-%h/.config}"/jellyfin-mpv-shim/cred.json | grep -o '"address": "[^"]*' | grep -o '[^"]*$'

This needs to be changed for multiple entries though.