Using PlexConnect_daemon

Move sleep 5 to here before CheckForNetwork:

Determine if the network is up by looking for any non-loopback network interfaces.

Currently supports only OSX “Darwin” OS

Sleep 5 <---------------------------------------------------Insert here


CheckForNetwork()

{

local test


if [ -z “${NETWORKUP:=}” ]; then

test=$(ifconfig -a inet 2>/dev/null | sed -n -e ‘/127.0.0.1/d’ -e ‘/0.0.0.0/d’ -e ‘/inet/p’ | wc -l)

if [ “${test}” -gt 0 ]; then

NETWORKUP=“-YES-”

else

NETWORKUP=“-NO-”

fi

fi

}

Determine if the operating system is OSX “darwin”. If so, then delay running the rest of

the script until the network is up. This avoids the binding of PlexConnect to the

loopback address which occurs if the bash script is called from a LaunchDaemon/plist

file at boot time.

Sleep 5 <----------------------------------------------------Remove from here


What you should be doing is entering in your startup script code into terminal one part at a time until you find out what is preventing it from starting at boot. Make sure plexconnect is stopped before you try. This is how I discovered my osx box was not working and required sleep 1 in plexconnect.bash on osx in order to load plexconnect @ boot on unix.

https://github.com/wahlmanj/PlexConnect/blob/master/update/OSX/PlexConnect.bash


Just out of curiosity why are you using a unix startup script in part of your code that specifically looks for Darwin os aka OSX? In the 2nd post i provided in your topic none of them seem to use unix startup scripts so why are you? Where did you even come up with this script? Start here:


Not Ubuntu but maybe this will help.https://forums.plex.tv/topic/97010-plexconnect-on-archlinux/

Also this which has Ubuntu:https://forums.plex.tv/topic/71220-linux-install-for-plex-connect/


Your startup script should look similar to this:


Below is the init script. This script is saved as /etc/init.d/plexconnect.

#!/bin/bash

BEGIN INIT INFO

Provides: plexconnect

Required-Start: plexmediaserver networking

Required-Stop: plexmediaserver 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


Now you need to make sure the script has the correct permissions and add it script as a service / daemon so that it starts automatically. Then start the daemon. To do this, make sure the permissions are correct and use update-rc.d. If you are using the root user you do not need the ‘sudo’ command at the front of the commands below.


sudo chown root:root /etc/init.d/plexconnect

sudo chmod 755 /etc/init.d/plexconnect

sudo update-rc.d plexconnect defaults

sudo /sbin/service plexconnect start

Move sleep 5 to here before CheckForNetwork:

I gave this a try and there was no change from before

Just out of curiosity why are you using a unix startup script in part of your code that specifically looks for Darwin os aka OSX? In the 2nd post i provided in your topic none of them seem to use unix startup scripts so why are you? Where did you even come up with this script? Start here:

Your startup script should look similar to this:

I don't recall specifically where I got the script from.  It was from one of the links early on in this thread...I think.  Anyways, I switched over to the script that you suggested and update-rc.d gave me errors about including runlevel 2.  The service doesn't seem to be starting either:

tom@HouseMedia:~$ sudo update-rc.d plexconnect defaults
update-rc.d: warning: plexconnect start runlevel arguments (2 3 4 5) do not match LSB Default-Start values (3 4 5)
 System start/stop links for /etc/init.d/plexconnect already exist.
tom@HouseMedia:~$ sudo /sbin/service plexconnect start
sudo: /sbin/service: command not found
tom@HouseMedia:~$ sudo service plexconnect start
 * PlexConnect process is not running
 * Starting the process PlexConnect
tom@HouseMedia:~$ sudo service plexconnect status
 * PlexConnect process is not running

Did you setup the test script to match your folder locations?

And why do you keep trying 

sudo /sbin/service plexconnect start

when its obvious that your service is not there? :-)

It should be in /usr/bin/

And theres not much we can do about this now, there is definitely something wrong with your server.

The script provided by plexconnect should work.

The only thing I could think of is that the network connection is not ready yet or something similar to this so that plexconnect cannot start at all. 

Spent some time setting this up to autostart and wanted to share a confirmed working approach for Ubuntu 12.04 given that this thread is not very easy to parse if you are not familiar with the inner working of Linux....the actual effort needed is minimal...

Open a terminal session and execute the following commands....

1) Create a symlink to the PlexConnect bash script in /etc/init.d/ (my install location for PlexConnect is /usr/local/lib/PlexConnect/PlexConnect-0.4 which you should change to reflect your install path)

  • sudo ln -s /usr/local/lib/PlexConnect/PlexConnect-0.4/PlexConnect_daemon.bash /etc/init.d/plexconnect

2) Update the run levels for the new service

  • sudo update-rc.d plexconnect defaults

3) Check that the service starts (it will fail silently if you don't use sudo)

  • sudo service plexconnect start
  • sudo service plexconnect status

You should see the message "PlexConnect is running"

4) Reboot your Ubuntu machine and check service has started using the service status command above or by accessing using the ATV

Hope this helps and thanks for a great piece of software

I'd just copy it there and nut put a symlink, but you can do it either way

For a long time now, Ubuntu has used a tool called upstart to launch processes when the system boots. Upstart is a massive improvement from the System V-style /sbin/init it replaced. Upstart doesn't need process to fork twice to daemonize themselves, nor does it need processes to log their pid files to some directory. Upstart is backwards compatible with the older scripts, which is why all the other stuff in this thread still works. If this is all mumbo jumbo to you, don't worry. This is much easier than everything else in this thread.

This works on Ubuntu for sure, I wouldn't recommend it for other Linux distributions. Everyone else has switched to an alternative startup mechanism called systemd. Ubuntu will also be switching to systemd soon (announcement made early in 2014 ).

But if you are using Ubuntu, this is a much easier and more reliable method. Put the following into the file /etc/init/plexconnect.conf (yes it needs to be owned by root, no it doesn't need to be executable):

# plexconnect
#
description "PlexConnect"

start on runlevel [2345]
stop on runlevel [!2345]

send standard console output to /dev/null

If you want logging add “logpath = /var/log” to PlexConnect’s Settings.cfg

console none

start it up, change path as necessary

exec /usr/local/src/PlexConnect/PlexConnect.py

You may need to adjust the last line to be the correct path depending on where you have installed PlexConnect. Once you have this file, you can manually start up PlexConnect by typing:

$ sudo initctl start plexconnect

To see if it's running type:

$ sudo initctl status plexconnect 

You can stop PlexConnect by typing:

$ sudo initctl stop plexconnect

Upstart will now start PlexConnect when your machine boots; it's also smart enough to notice if PlexConnect crashes and restart it.

Some people in this thread seem to have problems with PlexConnect starting before their network is ready. Upstart _should_ just take care of this for you, but if you continue to have problems, you can change the line in /etc/init/plexconnect.conf that says:

start on runlevel [2345]

to read:

start on (local-filesystems and net-device-up IFACE!=lo)

This tells upstart not to try and start PlexConnect until the local filesystems have been mounted and we have a network device other than localhost up and running.

Cheers.

For a long time now, Ubuntu has used a tool called upstart to launch processes when the system boots. Upstart is a massive improvement from the System V-style /sbin/init it replaced. Upstart doesn't need process to fork twice to daemonize themselves, nor does it need processes to log their pid files to some directory. Upstart is backwards compatible with the older scripts, which is why all the other stuff in this thread still works. If this is all mumbo jumbo to you, don't worry. This is much easier than everything else in this thread.

This works on Ubuntu for sure, I wouldn't recommend it for other Linux distributions. Everyone else has switched to an alternative startup mechanism called systemd. Ubuntu will also be switching to systemd soon (announcement made early in 2014 ).

But if you are using Ubuntu, this is a much easier and more reliable method. Put the following into the file /etc/init/plexconnect.conf (yes it needs to be owned by root, no it doesn't need to be executable):

# plexconnect
#
description "PlexConnect"

start on runlevel [2345]
stop on runlevel [!2345]

send standard console output to /dev/null

If you want logging add “logpath = /var/log” to PlexConnect’s Settings.cfg

console none

start it up, change path as necessary

exec /usr/local/src/PlexConnect/PlexConnect.py

You may need to adjust the last line to be the correct path depending on where you have installed PlexConnect. Once you have this file, you can manually start up PlexConnect by typing:

$ sudo initctl start plexconnect

To see if it's running type:

$ sudo initctl status plexconnect 

You can stop PlexConnect by typing:

$ sudo initctl stop plexconnect

Upstart will now start PlexConnect when your machine boots; it's also smart enough to notice if PlexConnect crashes and restart it.

Some people in this thread seem to have problems with PlexConnect starting before their network is ready. Upstart _should_ just take care of this for you, but if you continue to have problems, you can change the line in /etc/init/plexconnect.conf that says:

start on runlevel [2345]

to read:

start on (local-filesystems and net-device-up IFACE!=lo)

This tells upstart not to try and start PlexConnect until the local filesystems have been mounted and we have a network device other than localhost up and running.

Cheers.

@kotfu - this has been the only way I've been able to get plexconnect to start as a daemon on boot (in mint nadia/qiana).  Thanks very much!

@kotfu
 
So after upgrading from mint 14 to 17 (ubuntu 14.04 LTS), the script no longer works.  If I try starting it manually:

sudo initctl start plexconnect

I see two processes spawn (as opposed to 4, prior to upgrade) and then immediately die.  Looks like perms and ownership were retained, and PMS is running as normal.  Any ideas?

UPDATE: ubuntu betrayed me and installed/daemonized apache during the distro upgrade.. so plexconnect was fighting for port 80, and losing.  /shame

For a long time now, Ubuntu has used a tool called upstart to launch processes when the system boots. Upstart is a massive improvement from the System V-style /sbin/init it replaced. Upstart doesn't need process to fork twice to daemonize themselves, nor does it need processes to log their pid files to some directory. Upstart is backwards compatible with the older scripts, which is why all the other stuff in this thread still works. If this is all mumbo jumbo to you, don't worry. This is much easier than everything else in this thread.

This works on Ubuntu for sure, I wouldn't recommend it for other Linux distributions. Everyone else has switched to an alternative startup mechanism called systemd. Ubuntu will also be switching to systemd soon (announcement made early in 2014 ).

But if you are using Ubuntu, this is a much easier and more reliable method. Put the following into the file /etc/init/plexconnect.conf (yes it needs to be owned by root, no it doesn't need to be executable):

# plexconnect
#
description "PlexConnect"

start on runlevel [2345]
stop on runlevel [!2345]

send standard console output to /dev/null

If you want logging add “logpath = /var/log” to PlexConnect’s Settings.cfg

console none

start it up, change path as necessary

exec /usr/local/src/PlexConnect/PlexConnect.py

You may need to adjust the last line to be the correct path depending on where you have installed PlexConnect. Once you have this file, you can manually start up PlexConnect by typing:

$ sudo initctl start plexconnect

To see if it's running type:

$ sudo initctl status plexconnect 

You can stop PlexConnect by typing:

$ sudo initctl stop plexconnect

Upstart will now start PlexConnect when your machine boots; it's also smart enough to notice if PlexConnect crashes and restart it.

Some people in this thread seem to have problems with PlexConnect starting before their network is ready. Upstart _should_ just take care of this for you, but if you continue to have problems, you can change the line in /etc/init/plexconnect.conf that says:

start on runlevel [2345]

to read:

start on (local-filesystems and net-device-up IFACE!=lo)

This tells upstart not to try and start PlexConnect until the local filesystems have been mounted and we have a network device other than localhost up and running.

Cheers.

@kofu - I gave this a shot and plexconnect dies instantly I attempt to start it manually using initctl: 

tom@HouseMedia:~$ sudo initctl start plexconnect
plexconnect start/running, process 2974
tom@HouseMedia:~$ sudo initctl status plexconnect
plexconnect stop/waiting

 Syslog indicates what happened each time I attempt the manual start but I'm not sure what to do with this information:

HouseMedia init: plexconnect main process (2974) terminated with status 1

UPDATE1: Found a list of what the standard "status exit codes" are ~supposed~ to be:

			<span>0</span>
		
			program is running or service is OK
	
			<span>1</span>
		
			program is dead and /var/run pid file exists
	
			<span>2</span>
		
			program is dead and /var/lock lock file exists
	
			<span>3</span>
		
			program is not running
	
			<span>4</span>
		
			program or service status is unknown
	
			<span>5-99</span>
		
			reserved for future LSB use
	
			<span>100-149</span>
		
			reserved for distribution use
	
			<span>150-199</span>
		
			reserved for application use
	
			<span>200-254</span>
		
			reserved
	<p style="color:rgb(0,0,0);font-family:'Times New Roman';font-size:medium;">&nbsp;</p>

So the autostart job is "dead" and a PID file exists.  Not sure why a program would die but a PID would still be present...if that's what it actually means. 

I followed your instructions in this order:

1. sudo nano /etc/init/plexconnect.conf

2. Copy your script. I change the location of PlexConnect and it looks like this

# plexconnect
#
description "PlexConnect"

start on runlevel [2345]
stop on runlevel [!2345]

# send standard console output to /dev/null
# If you want logging add "logpath = /var/log" to PlexConnect's Settings.cfg
console none

# start it up, change path as necessary
exec /usr/local/lib/PlexConnect/PlexConnect.py

3. Restart. 

This does not work as I cannot Access the trailers. However, manual terminal command does start Plex - sudo initctl start plexconnect

I am running Ubuntu 14.04. Any idea why it does not start on its own?

Change the line referring to logging and then post what is output.  If you don't provide any details, it's unlikely anyone will be able to help you.

I followed your instructions in this order:

1. sudo nano /etc/init/plexconnect.conf

2. Copy your script. I change the location of PlexConnect and it looks like this

# plexconnect
#
description "PlexConnect"

start on runlevel [2345]
stop on runlevel [!2345]

# send standard console output to /dev/null
# If you want logging add "logpath = /var/log" to PlexConnect's Settings.cfg
console none

# start it up, change path as necessary
exec /usr/local/lib/PlexConnect/PlexConnect.py

3. Restart. 

This does not work as I cannot Access the trailers. However, manual terminal command does start Plex - sudo initctl start plexconnect

I am running Ubuntu 14.04. Any idea why it does not start on its own?

@kofu - I gave this a shot and plexconnect dies instantly I attempt to start it manually using initctl: 

tom@HouseMedia:~$ sudo initctl start plexconnect
plexconnect start/running, process 2974
tom@HouseMedia:~$ sudo initctl status plexconnect
plexconnect stop/waiting

 Syslog indicates what happened each time I attempt the manual start but I'm not sure what to do with this information:

HouseMedia init: plexconnect main process (2974) terminated with status 1

UPDATE1: Found a list of what the standard "status exit codes" are ~supposed~ to be:

0 program is running or service is OK 1 program is dead and /var/run pid file exists 2 program is dead and /var/lock lock file exists 3 program is not running 4 program or service status is unknown 5-99 reserved for future LSB use 100-149 reserved for distribution use 150-199 reserved for application use 200-254 reserved

 

So the autostart job is "dead" and a PID file exists.  Not sure why a program would die but a PID would still be present...if that's what it actually means. 

If PlexConnect is dying instantly when started from upstart, then it's likely one of two problems.

The first troubleshooting step for any autostart problems is to make sure the process starts manually. Make sure you can start PlexConnect.py without using upstart. So do something like:

$ sudo /usr/local/lib/PlexConnect/PlexConnect.py

and watch the output on your screen closely. If you are getting an immediate exit from upstart, it's pretty likely that PlexConnect is having issues on it's own.

Second thing to try is to make sure you have the right path in plexconnect.conf. Everybody makes a typo now and then, just triple check to make sure you have the path correct. Copy and paste is a good method to validate this: copy the path out of the config file and paste it onto the command line. If you have the path wrong, upstart will be failing with an exit code 1 because it can't find anything to run.

If neither of these things helps you figure it out, then please add the following line to PlexConnect/Settings.cfg:

logpath = /var/log

Then try starting via upstart with:

$ sudo initctl start plexconnect

PlexConnect will create a log file in /var/log/PlexConnect.log. Post that here and we'll see what we can do to help you out.

@kotfu
 
So after upgrading from mint 14 to 17 (ubuntu 14.04 LTS), the script no longer works.  If I try starting it manually:

sudo initctl start plexconnect

I see two processes spawn (as opposed to 4, prior to upgrade) and then immediately die.  Looks like perms and ownership were retained, and PMS is running as normal.  Any ideas?

UPDATE: ubuntu betrayed me and installed/daemonized apache during the distro upgrade.. so plexconnect was fighting for port 80, and losing.  /shame

Glad you figured it out.

This has finally worked for me on Ubuntu 14.04

add the following line to /etc/rc.local

 
python /usr/local/lib/PlexConnect/PlexConnect.py

This has finally worked for me on Ubuntu 14.04

add the following line to /etc/rc.local

 
python /usr/local/lib/PlexConnect/PlexConnect.py

same here, it's not clean but works perfectly (12.04 lts)

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

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.