Auto Start PlexConnect on Ubuntu

plexconnect

#1

I have been trying to get the daemon to start on boot on my Ubuntu server, but I can not get it working. There is a link on the PlexConnect setup instructions to do this, but the link seems to be broken.

I have PlexConnect working great, but I have to run the PlexConnect_deamon.bash start command everytime I reboot my system.


#2

Make sure you have screen installed:

sudo apt-get install screen

Create a file called plexconnect in /etc/init.d:

sudo nano /etc/init.d/plexconnect

Give it the following content (update the DAEMON_OPTS="-S PlexConnect -d -m /usr/local/lib/PlexConnect/PlexConnect.py" line so it will point to your installation of plexconnect):

#!/bin/bash
### BEGIN INIT INFO
# Provides:          plexconnect
# Required-Start:    networking
# Required-Stop:    networking
# Default-Start:     3 4 5
# Default-Stop:      0 1 6
# Short-Description: This is the Plex Connect daemon
# Description:       This script starts the Plex Connect
#                    Python scripts in a detached screen.
### END INIT INFO

# Using the lsb functions to perform the operations.
. /lib/lsb/init-functions

# Process name ( For display )
NAME=PlexConnect

# Daemon name, where is the actual executable
DAEMON="/usr/bin/screen"
DAEMON_OPTS="-S PlexConnect -d -m /usr/local/lib/PlexConnect/PlexConnect.py"
DAEMON_USER="root"

# pid file for the daemon
PIDFILE=/var/run/PlexConnect.pid

# If the daemon is not there, then exit.
test -x "$DAEMON" || exit 5

case $1 in
 start)
  # Checked the PID file exists and check the actual status of process
  if [ -e $PIDFILE ]; then
   status_of_proc -p $PIDFILE "$DAEMON $DAEMON_OPTS" "$NAME process" && status="0" || status="$?"
   # If the status is SUCCESS then don't need to start again.
   if [ $? = "0" ]; then
    log_success_msg "Starting the process $NAME"
    exit # Exit
   fi
  fi
  # Start the daemon.
  # Start the daemon with the help of start-stop-daemon
  # Log the message appropriately
  if start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE --startas $DAEMON -p $PIDFILE -- ${DAEMON_OPTS}; then
   while read line ; do [[ $line =~ ([0-9]*).PlexConnect ]] && echo ${BASH_REMATCH[1]} ; done < <(screen -ls) > $PIDFILE
   log_success_msg "Starting the process $NAME"
  else
   log_failure_msg "Starting the process $NAME"
  fi
  ;;
 stop)

  # Stop the daemon.
  if [ -e $PIDFILE ]; then
   status_of_proc -p $PIDFILE "$DAEMON DAEMON_OPTS" "Stoppping the $NAME process" && status="0" || status="$?"
   if [ "$?" = 0 ]; then
    start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
    /bin/rm -rf $PIDFILE
    log_success_msg ""Stopping the $NAME process""
   fi
  else
   log_failure_msg "$NAME process is not running"
  fi
  ;;
 restart)
  # Restart the daemon.
  $0 stop && sleep 2 && $0 start
  ;;
 status)
  # Check the status of the process.
  if [ -e $PIDFILE ]; then
   status_of_proc -p $PIDFILE "$DAEMON $DAEMON_OPTS" "$NAME process" && exit 0 || exit $?
   log_success_msg "$NAME process is running"
  else
   log_failure_msg "$NAME process is not running"
  fi
  ;;
 reload)
  $0 restart
  ;;
 *)
  # For invalid arguments, print the usage message.
  echo "Usage: $0 {start|stop|restart|reload|status}"
  exit 2
  ;;
esac

Make the file executable

sudo chmod 755 /etc/init.d/plexconnect

And check whether it works (make sure plexconnect is not running before starting this one):

sudo service plexconnect start

Make sure the script starts automatically when you reboot:

sudo update-rc.d plexconnect defaults


#3

I have done this and I am receiving this error when trying to start or check the status of the service:
/etc/init.d/plexconnect: invalid arguments
* could not access PID file for PlexConnect process


#4

Are you sure you have started the service as root?

sudo service plexconnect start

The "sudo" is the important part here.

Check whether the file /var/run/PlexConnect.pid exists and what security it has:

ls -al /var/run/PlexConnect.pid

If it doesn't exist, then plexconnect was probably not started as root (the sudo part). If it does exist, kill everything related to PlexConnect:

ps -ef | grep -i plexconnect

should not show any plexconnect processes anymore. Then remove the /var/run/PlexConnect.pid file:

rm /var/run/PlexConnect.pid

and start the service again:

sudo service plexconnect start


#5

So that all works well enough and the service starts, but then if I try to read the status using "sudo service plexconnect status" I get the same error. I can only assume that the cause of the error message is also the reason that the service is not running on boot.

I have used sudo for all of the instructions so far. The PID file created when I started the service again has these permissions "-rw-r--r-- 1 root root 5 Oct 3 20:07 /var/run/PlexConnect.pid"


#6

Right now I am bypassing this by deleting the pid file then starting the service with rc.local. Not a perfect solution as I still can not check the status of the process with sudo service plexconnect status.


#7

Hey, I have also a problem. I did what you said and i think everthing is running fine so far, but maybe the process is booting to early because it has no internet connection.

Here is the status i get:

● plexconnect.service - PlexConnect for Linux
Loaded: loaded (/etc/systemd/system/plexconnect.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sun 2015-10-04 12:29:59 CEST; 1min 57s ago
Process: 610 ExecStart=/usr/bin/python /usr/local/lib/PlexConnect/PlexConnect.py (code=exited, status=1/FAILURE)
Main PID: 610 (code=exited, status=1/FAILURE)

Oct 04 12:29:59 ubuntuserver python[610]: File "/usr/local/lib/PlexConnect/PlexConnect.py", line 82, in startup
Oct 04 12:29:59 ubuntuserver python[610]: param['IP_self'] = getIP_self()
Oct 04 12:29:59 ubuntuserver python[610]: File "/usr/local/lib/PlexConnect/PlexConnect.py", line 31, in getIP_self
Oct 04 12:29:59 ubuntuserver python[610]: s.connect(('1.2.3.4', 1000))
Oct 04 12:29:59 ubuntuserver python[610]: File "/usr/lib/python2.7/socket.py", line 228, in meth
Oct 04 12:29:59 ubuntuserver python[610]: return getattr(self._sock,name)(args)
**Oct 04 12:29:59 ubuntuserver python[610]: socket.error: [Errno 101] Network is unreachable
*
Oct 04 12:29:59 ubuntuserver systemd[1]: plexconnect.service: main process exited, code=exited, status=1/FAILURE
Oct 04 12:29:59 ubuntuserver systemd[1]: Unit plexconnect.service entered failed state.
Oct 04 12:29:59 ubuntuserver systemd[1]: plexconnect.service failed.

hope you can help me


#8

That should not happen. The line:

# Required-Start: networking

Should force the script to start after the networking service. I am not sure if your are on WiFi. If so, the networking service might have started, but the WiFi is still negotiating its connection.

In that case, you could do the following:
After the line that says

start)

enter a line with the following:

sleep 5

That should force the service to wait 5 seconds before it starts. Not ideal, but might be a good workaround.


#9

@sap995 said:
So that all works well enough and the service starts, but then if I try to read the status using "sudo service plexconnect status" I get the same error. I can only assume that the cause of the error message is also the reason that the service is not running on boot.

I have used sudo for all of the instructions so far. The PID file created when I started the service again has these permissions "-rw-r--r-- 1 root root 5 Oct 3 20:07 /var/run/PlexConnect.pid"

Could you please kill all the processe and remove the pid-file and then start the service again (not the rc.local version, but the real service).
Then see what the following command outputs:

ps -ef | grep `sudo cat /var/run/PlexConnect.pid`

It should show you a screen process and a plexconnect.py process. If it is not, then your service won't start at all.


#10

I'm running into a different problem than whats listed above. When I run sudo service PlexConnect start I get an error stating Failed to start PlexConnect.service: Unit PlexConnect.service failed to load: No such file or directory.

I've got PlexConnect installed in: /usr/local/lib/PlexConnect and it's permissions are drwxr-xr-x 5 root root 4096 Oct 8 19:49 PlexConnect/ so I would think that the service running as root would be able to read and execute PlexConnect.

Screen is installed screen: /usr/bin/screen /usr/share/screen /usr/share/man/man1/screen.1.gz

init-functions appears to exist at the correct location /u/l/lib> ls /lib/lsb/
init-functions init-functions.d/

/var/run/ exists though it does not contain a PlexConnect.pid this doesn't surprise me as it's not starting.

Those are the only paths I've identified in the init script above so I must be doing something wrong, I'm just not sure what it is. Any help would be greatly appreciated.


#11

I figgured it out. The instruction Make sure the script starts automatically when you reboot:
sudo update-rc.d plexconnect defaults
is not just to make sure int he script starts it's to install the script so that upstart knows that the service exists. You can't start or stop it at all until it's been installed.


#12

@dlgreenwald said:
I figgured it out. The instruction Make sure the script starts automatically when you reboot:
sudo update-rc.d plexconnect defaults
is not just to make sure int he script starts it's to install the script so that upstart knows that the service exists. You can't start or stop it at all until it's been installed.

That might be true, a lot has changed since the introduction of systemd and I am not completely up-to-speed with those changes.


#13

I had the same problem as dlgreenwald. I solved it by instead of doing sudo update-rc.d plexconnect defaults I made a symlink like this
sudo ln -s /etc/init.d/plexconnect /etc/rc2.d/S99plexconnect.
And in /etc/init.d/plexconnect I changed the row
# Default-Start: 3 4 5
to
# Default-Start: 2 3 4 5

To undo the sudo update-rc.d plexconnect defaults just run sudo update-rc.d plexconnect remove


#14

Hi all,

trying to get this to work on a Raspberry Pi. I can manually start and stop the script, however, it won't start on boot. Tried rc.d, rc.local and cron. Any reason why this script shouldn't be working on a Pi running Raspbian? Might it have something to do with the user used to run the daemon?

Any help appreciated!

@SingleServingSociety said:
Make sure you have screen installed:

sudo apt-get install screen

Create a file called plexconnect in /etc/init.d:

sudo nano /etc/init.d/plexconnect

Give it the following content (update the DAEMON_OPTS="-S PlexConnect -d -m /usr/local/lib/PlexConnect/PlexConnect.py" line so it will point to your installation of plexconnect):

#!/bin/bash
### BEGIN INIT INFO
# Provides:          plexconnect
# Required-Start:    networking
# Required-Stop:    networking
# Default-Start:     3 4 5
# Default-Stop:      0 1 6
# Short-Description: This is the Plex Connect daemon
# Description:       This script starts the Plex Connect
#                    Python scripts in a detached screen.
### END INIT INFO

# Using the lsb functions to perform the operations.
. /lib/lsb/init-functions

# Process name ( For display )
NAME=PlexConnect

# Daemon name, where is the actual executable
DAEMON="/usr/bin/screen"
DAEMON_OPTS="-S PlexConnect -d -m /usr/local/lib/PlexConnect/PlexConnect.py"
DAEMON_USER="root"

# pid file for the daemon
PIDFILE=/var/run/PlexConnect.pid

# If the daemon is not there, then exit.
test -x "$DAEMON" || exit 5

case $1 in
 start)
  # Checked the PID file exists and check the actual status of process
  if [ -e $PIDFILE ]; then
   status_of_proc -p $PIDFILE "$DAEMON $DAEMON_OPTS" "$NAME process" && status="0" || status="$?"
   # If the status is SUCCESS then don't need to start again.
   if [ $? = "0" ]; then
    log_success_msg "Starting the process $NAME"
    exit # Exit
   fi
  fi
  # Start the daemon.
  # Start the daemon with the help of start-stop-daemon
  # Log the message appropriately
  if start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE --startas $DAEMON -p $PIDFILE -- ${DAEMON_OPTS}; then
   while read line ; do [[ $line =~ ([0-9]*).PlexConnect ]] && echo ${BASH_REMATCH[1]} ; done < <(screen -ls) > $PIDFILE
   log_success_msg "Starting the process $NAME"
  else
   log_failure_msg "Starting the process $NAME"
  fi
  ;;
 stop)

  # Stop the daemon.
  if [ -e $PIDFILE ]; then
   status_of_proc -p $PIDFILE "$DAEMON DAEMON_OPTS" "Stoppping the $NAME process" && status="0" || status="$?"
   if [ "$?" = 0 ]; then
    start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
    /bin/rm -rf $PIDFILE
    log_success_msg ""Stopping the $NAME process""
   fi
  else
   log_failure_msg "$NAME process is not running"
  fi
  ;;
 restart)
  # Restart the daemon.
  $0 stop && sleep 2 && $0 start
  ;;
 status)
  # Check the status of the process.
  if [ -e $PIDFILE ]; then
   status_of_proc -p $PIDFILE "$DAEMON $DAEMON_OPTS" "$NAME process" && exit 0 || exit $?
   log_success_msg "$NAME process is running"
  else
   log_failure_msg "$NAME process is not running"
  fi
  ;;
 reload)
  $0 restart
  ;;
 *)
  # For invalid arguments, print the usage message.
  echo "Usage: $0 {start|stop|restart|reload|status}"
  exit 2
  ;;
esac

Make the file executable

sudo chmod 755 /etc/init.d/plexconnect

And check whether it works (make sure plexconnect is not running before starting this one):

sudo service plexconnect start

Make sure the script starts automatically when you reboot:

sudo update-rc.d plexconnect defaults


#15

It seems there is no clear guide to make plexconnect autostart in ubuntu, however after much searching I finally found this blog

Edit /etc/rc.local and add /usr/local/lib/PlexConnect_daemon.bash start

Then reboot and check status:
cd /usr/local/lib/PlexConnect
sudo ./PlexConnect_daemon.bash status

This worked for me on Ubuntu 14.04 LTS


#16

@sap995 said:
I have done this and I am receiving this error when trying to start or check the status of the service:
/etc/init.d/plexconnect: invalid arguments
* could not access PID file for PlexConnect process
Change line 72 of /etc/init.d/plexconnect from
status_of_proc -p $PIDFILE "$DAEMON $DAEMON_OPTS" "$NAME process" && exit 0 || exit $?
to
status_of_proc -p $PIDFILE "$DAEMON" $NAME && exit 0 || exit $?
And you should be good to go.


#17

Thanks guys. I should have updated sooner, but I was gifted one of the new ATV 4s. I feel like plex and the ATV4 were meant for each other. My media experience is now, dare I say it, wife proof.

Everything is working great. Hopefully the solutions above will be helpful to other users. If not I suggest other Noobs like myself just get an Apple TV 4.


#18

@cyberopus said:

@sap995 said:
I have done this and I am receiving this error when trying to start or check the status of the service:
/etc/init.d/plexconnect: invalid arguments
* could not access PID file for PlexConnect process
Change line 72 of /etc/init.d/plexconnect from
status_of_proc -p $PIDFILE "$DAEMON $DAEMON_OPTS" "$NAME process" && exit 0 || exit $?
to
status_of_proc -p $PIDFILE "$DAEMON" $NAME && exit 0 || exit $?
And you should be good to go.

I'm running PlexConnect on Raspberry Pi 3 running an up to date version of Raspbian Jessie. After making your suggested change to /etc/init.d/plexconnect I was able successfully start PlexConnect as a service. However, it will not auto start after a reboot.

After a reboot /var/log/daemon.log shows no obvious errors

Mar  8 19:34:28 Pi3 systemd[1]: Starting LSB: Plex Media Server...
Mar  8 19:34:28 Pi3 systemd[1]: Starting LSB: This is the Plex Connect daemon...
Mar  8 19:34:28 Pi3 plexconnect[380]: Starting the process PlexConnect.
Mar  8 19:34:28 Pi3 systemd[1]: Started LSB: This is the Plex Connect daemon.
Mar  8 19:34:29 Pi3 plexmediaserver[379]: Starting Plex Media Server: done
Mar  8 19:34:29 Pi3 systemd[1]: Started LSB: Plex Media Server.

PlexConnect.log gets written to but never actually starts

19:34:30 PlexConnect: started: 19:34:30
19:34:30 PlexConnect: Version: 0.5-dev-101215
19:34:30 PlexConnect: Python: 2.7.9 (default, Mar  8 2015, 00:52:26) 
[GCC 4.9.2]
19:34:30 PlexConnect: Host OS: linux2
19:34:30 PlexConnect: PILBackgrounds: Is PIL installed? True

a PID gets written to /var/run/PlexConnect.pid during boot, but after boot it's not running, which probably explains this error in /var/log/syslog when I stop the PlexConnect service.

Mar  8 19:55:16 Pi3 systemd[1]: Stopping LSB: This is the Plex Connect daemon...
Mar  8 19:55:16 Pi3 plexconnect[2027]: /etc/init.d/plexconnect: invalid arguments
Mar  8 19:55:16 Pi3 plexconnect[2027]: could not access PID file for Stoppping the PlexConnect process ... failed!
Mar  8 19:55:16 Pi3 plexconnect[2027]: start-stop-daemon: warning: failed to kill 440: No such process
Mar  8 19:55:16 Pi3 plexconnect[2027]: Stopping the PlexConnect process.
Mar  8 19:55:16 Pi3 systemd[1]: Stopped LSB: This is the Plex Connect daemon. 

But when I manually start the service it all works

/var/log/syslog

Mar  8 19:55:16 Pi3 systemd[1]: Stopped LSB: This is the Plex Connect daemon.
Mar  8 19:57:47 Pi3 systemd[1]: Starting LSB: This is the Plex Connect daemon...
Mar  8 19:57:47 Pi3 plexconnect[2168]: Starting the process PlexConnect.
Mar  8 19:57:47 Pi3 systemd[1]: Started LSB: This is the Plex Connect daemon.

/opt/PlexConnect-master/PlexConnect.log

19:57:47 DNSServer: DNSServer: Serving DNS on 192.168.1.240 port 53.
19:57:47 DNSServer: ***
19:57:48 WebServer: started: 19:57:48
19:57:48 WebServer: ***
19:57:48 WebServer: WebServer: Serving HTTP on 192.168.1.240 port 80.
19:57:48 WebServer: ***
19:57:48 WebServer: started: 19:57:48
19:57:48 WebServer: ***
19:57:48 WebServer: WebServer: Serving HTTPS on 192.168.1.240 port 443.
19:57:48 WebServer: ***

Any idea what might be causing the service to fail at boot time?


#19

On Ubuntu 15.10 RPi3 this worked for me:
add the following line to /etc/rc.local
/usr/local/lib/PlexConnect/PlexConnect_daemon.bash start


#20

@mattbryce said:
On Ubuntu 15.10 RPi3 this worked for me:
add the following line to /etc/rc.local
/usr/local/lib/PlexConnect/PlexConnect_daemon.bash start

Cheers for the tip, I added that line but I'm still getting this in PlexConnect.log

21:18:16 PlexConnect: Python: 2.7.9 (default, Mar  8 2015, 00:52:26) 
[GCC 4.9.2]
21:18:16 PlexConnect: Host OS: linux2
21:18:16 PlexConnect: PILBackgrounds: Is PIL installed? True
21:18:16 PlexConnect: started: 21:18:16
21:18:16 PlexConnect: Version: 0.5-dev-101215
21:18:16 PlexConnect: Python: 2.7.9 (default, Mar  8 2015, 00:52:26) 
[GCC 4.9.2]
21:18:16 PlexConnect: Host OS: linux2
21:18:16 PlexConnect: PILBackgrounds: Is PIL installed? True