Search code examples
linuxubuntusystemd

Auto-starting Twonky Server on Ubuntu 18.04 using systemd


I was trying to set up a Twonky Server on Ubuntu. The server works fine, but I could not get systemd to autostart the server (using a service file I created at /etc/systemd/system/twonkyserver.service). Sometimes I got the cryptic error message that some PID-file (/var/run/mediaserver.pid) is not accessible, the exit code of the service is 13, which apparently is a EACCES Permission denied error . The service runs as root.

I finally managed to fix the problem by setting PIDFile in the twonkyserver.service file to /var/run/mediaserver.pid. For reference, find the service file below:

[Unit]
Description=Twonky Server Service
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/twonky/twonky.sh start
ExecStop=/usr/local/twonky/twonky.sh stop
ExecReload=/usr/local/twonky/twonky.sh reload
ExecRestart=/usr/local/twonky/twonky.sh restart
PIDFile=/var/run/mediaserver.pid
Restart=on-failure

[Install]
WantedBy=multi-user.target

Solution

  • As described above, the below service file auto-starts the Twonky Server on boot. Simply create it using vim /etc/systemd/system/twonkyserver.service. This assumses that you have installed the Twonky Server to usr/local/twonky. The shell-file twonky.sh already provides a nice interface to the service file (twonky.sh start|stop|reload|restart, also see twonky.sh -h).

    [Unit]
    Description=Twonky Server Service
    After=network.target
    
    [Service]
    Type=simple
    ExecStart=/usr/local/twonky/twonky.sh start
    ExecStop=/usr/local/twonky/twonky.sh stop
    ExecReload=/usr/local/twonky/twonky.sh reload
    ExecRestart=/usr/local/twonky/twonky.sh restart
    PIDFile=/var/run/mediaserver.pid
    Restart=on-failure
    
    [Install]
    WantedBy=multi-user.target