INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: Systemd services to autostart JRiver and a VNC server  (Read 9369 times)

mwillems

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 5234
  • "Linux Merit Badge" Recipient
Systemd services to autostart JRiver and a VNC server
« on: January 19, 2017, 10:54:31 am »

So I've been working on automating more of my JRiver config (more to come on this in the next version of the pi isntall guide), but in the meanwhile I thought I'd put some useful config files here for everyone's reference

Here's a systemd service that will automatically start jriver on boot as your user (after the display manager loads), and if JRiver should crash, restarts it within 10 seconds.  This will work best on systems with autologin, but because of the restarting feature, should work fine even if it has to wait for you to login.  You want to put your username in the "user" spot, and put the following text in a file at /etc/systemd/system/jriver.service:
Code: [Select]
  [Unit]
     Description=JRiver
     After=display-manager.service

  [Service]
     Type=simple
     Environment=DISPLAY=:0
     User=your_user_here
     ExecStart=/usr/bin/mediacenter22 /MediaServer
     Restart=always
     RestartSec=10
     KillSignal=SIGHUP
     TimeoutStopSec=45

  [Install]
     WantedBy=graphical.target

Some technical notes:  this presumes you're running on your actual display, not a virtual display generated by a VNC client.  If you are using such a virtual display, substitute the display variable in the Environment section, and change the "After" line to refer to the name of the systemd service you're using to start your vnc server, but note that VNC virtual displays may not work correctly anymore with MC since 22.0.36.

In that vein, here's a service to start x11vnc to allow remote access to your actual display on boot.  It similarly will restart if it crashes.  You want to put your username in place of both of the "your_user_here" placeholders below, create a file at /etc/systemd/system/x11vnc.service, and put the following text there:

Code: [Select]
[Unit]
     Description=x11vnc
     After=display-manager.service

  [Service]
     Type=forking
     Environment=DISPLAY=:0
     User=your_user_here
     ExecStart=/usr/bin/x11vnc -rfbauth /home/your_user_here/.vnc/passwd -display :0 -geometry 1920x1080 -auth guess -forever -bg
     Restart=always
     RestartSec=10

  [Install]
     WantedBy=graphical.target

Technical notes: this presumes that you've already setup a VNC password.  If you want to run without a password remove the -rfbauth flag and the path following it.  If you want to set a password run the following command replacing "your_password" with your password:

Code: [Select]
x11vnc -storepasswd your_password ~/.vnc/passwd

Once you've got the services in place you can set them to start on boot by typing
Code: [Select]
sudo systemctl enable jriver.service
and/or
Code: [Select]
sudo systemctl enable x11vnc.service

As noted above, at some point I'm going to overhaul the pi startup instructions to incorporate these (and to add more scripting to automate it), but I thought these might be of general interest in the meanwhile.
Logged

mk9pa

  • Recent member
  • *
  • Posts: 22
Re: Systemd services to autostart JRiver and a VNC server
« Reply #1 on: January 21, 2017, 11:16:47 pm »

Excellent guide.  I was just looking for this tonight, and you posted it 2 days ago.  Good timing.


I implemented your directions on my RPi3 setup and it works perfectly.
Logged

bob

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 13870
Re: Systemd services to autostart JRiver and a VNC server
« Reply #2 on: January 30, 2017, 02:48:30 pm »

It would be nice to have it handle shutdown as well if that's possible.

If you send MC a HUP signal it will initiate a clean shutdown which includes notifying all DLNA devices that it's going down as well as unsubscribing from events and closing all open files. You need to wait until MC finishes it's shutdown before continuing. This can take up to 30 seconds on a network with a ton of DLNA devices.

In init on the Id I just wait until the mediacenter22 process ends.

Logged

mk9pa

  • Recent member
  • *
  • Posts: 22
Re: Systemd services to autostart JRiver and a VNC server
« Reply #3 on: January 30, 2017, 03:54:31 pm »

Hi Bob / MWillems,


I added the following two lines to the [Service] stanza of MWillem's jriver.service file and it seems to have done the trick:


Code: [Select]
  KillMode=process
  KillSignal=SIGHUP

The full jriver.service file is now:

Code: [Select]
[Unit]
  Description=JRiver
  After=display-manager.service


[Service]
  Type=simple
  Environment=DISPLAY=:0
  User=pi
  ExecStart=/usr/bin/mediacenter22 /MediaServer
  Restart=always
  RestartSec=10
  KillMode=process
  KillSignal=SIGHUP


[Install]
  WantedBy=graphical.target


Looking at /var/log/daemon.log after I type service jriver stop, I see:
Code: [Select]
Jan 30 13:46:48 audiopi systemd[1]: Stopping JRiver...
Jan 30 13:46:48 audiopi mediacenter22[9644]: Received a hangup, exiting!
Jan 30 13:46:55 audiopi systemd[1]: Stopped JRiver.

Logged

bob

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 13870
Re: Systemd services to autostart JRiver and a VNC server
« Reply #4 on: January 30, 2017, 04:29:28 pm »

Hi Bob / MWillems,


I added the following two lines to the [Service] stanza of MWillem's jriver.service file and it seems to have done the trick:


Code: [Select]
  KillMode=process
  KillSignal=SIGHUP

The full jriver.service file is now:

Code: [Select]
[Unit]
  Description=JRiver
  After=display-manager.service


[Service]
  Type=simple
  Environment=DISPLAY=:0
  User=pi
  ExecStart=/usr/bin/mediacenter22 /MediaServer
  Restart=always
  RestartSec=10
  KillMode=process
  KillSignal=SIGHUP


[Install]
  WantedBy=graphical.target


Looking at /var/log/daemon.log after I type service jriver stop, I see:
Code: [Select]
Jan 30 13:46:48 audiopi systemd[1]: Stopping JRiver...
Jan 30 13:46:48 audiopi mediacenter22[9644]: Received a hangup, exiting!
Jan 30 13:46:55 audiopi systemd[1]: Stopped JRiver.

That looks good and hopefully it waits until MC ends if it's executed during the system shutdown and reboot processes.
Logged

mwillems

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 5234
  • "Linux Merit Badge" Recipient
Re: Systemd services to autostart JRiver and a VNC server
« Reply #5 on: January 30, 2017, 05:18:02 pm »

It's hard to convince systemd to wait indefinitely, but you can specify a timeout with:
Code: [Select]
TimeoutStopSec=45

Adding that with Killsignal set to SIGHUP waits for JRiver to finish or for 45 seconds whichever comes first.  I tested on a VM, with just the kill signal but no timeout, the shutdown is instantaneous; with the timeout it waits about five or six seconds, at which point a log message shows jriver closing and the shutdown finishes (a half second later).

You don't need the killmode.  I'll add the extra lines to the service in the top post.
Logged

mk9pa

  • Recent member
  • *
  • Posts: 22
Re: Systemd services to autostart JRiver and a VNC server
« Reply #6 on: January 30, 2017, 05:43:44 pm »

It's hard to convince systemd to wait indefinitely, but you can specify a timeout with:
Code: [Select]
TimeoutStopSec=45

Adding that with Killsignal set to SIGHUP waits for JRiver to finish or for 45 seconds whichever comes first.  I tested on a VM, with just the kill signal but no timeout, the shutdown is instantaneous; with the timeout it waits about five or six seconds, at which point a log message shows jriver closing and the shutdown finishes (a half second later).

You don't need the killmode.  I'll add the extra lines to the service in the top post.
Minor point -- the timeout shouldn't be necessary.  If not provided, it defaults to DefaultTimeoutStopSec which is 90s (https://www.freedesktop.org/software/systemd/man/systemd-system.conf.html# and https://www.freedesktop.org/software/systemd/man/systemd.service.html, and see the file /etc/systemd/system.conf on your system). The log file snippet above shows a 7 second delay between systemd issuing the signal and detecting that MC has exited, which is about what it felt like at the command line.
Logged

mwillems

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 5234
  • "Linux Merit Badge" Recipient
Re: Systemd services to autostart JRiver and a VNC server
« Reply #7 on: January 30, 2017, 05:49:18 pm »

Minor point -- the timeout shouldn't be necessary.  If not provided, it defaults to DefaultTimeoutStopSec which is 90s (https://www.freedesktop.org/software/systemd/man/systemd-system.conf.html# and https://www.freedesktop.org/software/systemd/man/systemd.service.html, and see the file /etc/systemd/system.conf on your system). The log file snippet above shows a 7 second delay between systemd issuing the signal and detecting that MC has exited, which is about what it felt like at the command line.

That's what is supposed to happen, but is not actually happening on the systems I tested.  It may be a bug, but without the timeout systemd just shuts on down on my testbeds.  It may also be distro-specific changes to defaults (I'm testing on the debian stretch release candidate), or (as happens occasionally with systemd) there may have been undocumented changes.

In any case, if one configuration needs it others may  ;D
Logged

Mike Noe

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 792
Re: Systemd services to autostart JRiver and a VNC server
« Reply #8 on: May 15, 2017, 07:42:59 am »

@mwillems

Just FYI, the "TimeoutStopSec" entry in jriver.service in the first post is missing the "t"  (TimeouStopSec).
Logged
openSUSE TW/Plasma5 x86_64 | Win10Pro/RX560
S.M.S.L USB-DAC => Transcendent GG Pre (kit) => Transcendent mono OTLs (kit)
(heavily modded) Hammer Dynamics Super-12s (kit)
(optionally) VonSchweikert VR8s

mwillems

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 5234
  • "Linux Merit Badge" Recipient
Re: Systemd services to autostart JRiver and a VNC server
« Reply #9 on: May 15, 2017, 04:32:45 pm »

Good catch!  Thanks.
Logged
Pages: [1]   Go Up