Using PlexConnect_daemon

I like it :)

yea, you created a symlink to /path/to/plexconnect/ which of course does not exist :D

You should replace that path with /home/tom/PlexConnect-master/PlexConnect_daemon.bash

ls -la

command in /etc/init.d/ will show that you basically directed that link to a nonexistent file. It does of course now exist but it doesnt point to anything so it actually does not exist :)

Removed the plexconnect symlink & corrected my super-embarrassing typo:
tom@HouseMedia:/etc/init.d$ sudo ln -s /home/tom/PlexConnect-master/PlexConnect_daemon.bash /etc/init.d/plexconnect

Checking the symlinks within /etc/init.d (using $ls -la) I see that it makes sense now (and double-checked that the execution bit is set):

lrwxrwxrwx   1 root root    52 2014-02-17 11:22 plexconnect -> /home/tom/PlexConnect-master/PlexConnect_daemon.bash

update-rc.d was still complaining about the plexconnect file not existing...and then I noticed that a previous mistake was catching up with me:

lrwxrwxrwx  1 tom  tom       23 2014-02-15 16:19 PlexConnect_daemon.bash -> /etc/init.d/plexconnect

Thankfully I kept a backup copy of the .bash file so I simply restored it as PlexConnect_daemon.bash.  Now update-rc.d throws a warning that I'm not sure if I should be concerned about (since it's a warning, not an error)...and the start/stop links are, of course, still exist:

tom@HouseMedia:/etc/init.d$ sudo update-rc.d plexconnect defaults
update-rc.d: warning: /etc/init.d/plexconnect missing LSB information
update-rc.d: see 
 System start/stop links for /etc/init.d/plexconnect already exist.

Using $service plexconnect start|stop|restart now works as it should but after restarting the system, the service is not starting on it's own.

 
Not sure how to go about troubleshooting this.  Don't really understand this runlevel stuff but it looks like it should have been executed because it shows up in the runlevel 2 folder:
$ls -l /etc/rc2.d
lrwxrwxrwx 1 root root  21 2014-02-07 19:12 S20plexconnect -> ../init.d/plexconnect

So I thought I'd re-add the start/stop links (and restart the system) to see if that made a difference:

tom@HouseMedia:~/PlexConnect-master$ sudo update-rc.d -f plexconnect remove
 Removing any system startup links for /etc/init.d/plexconnect ...
   /etc/rc0.d/K20plexconnect
   /etc/rc1.d/K20plexconnect
   /etc/rc2.d/S20plexconnect
   /etc/rc3.d/S20plexconnect
   /etc/rc4.d/S20plexconnect
   /etc/rc5.d/S20plexconnect
   /etc/rc6.d/K20plexconnect
tom@HouseMedia:~/PlexConnect-master$ sudo update-rc.d plexconnect defaults
update-rc.d: warning: /etc/init.d/plexconnect missing LSB information
update-rc.d: see 
 Adding system startup for /etc/init.d/plexconnect ...
   /etc/rc0.d/K20plexconnect -> ../init.d/plexconnect
   /etc/rc1.d/K20plexconnect -> ../init.d/plexconnect
   /etc/rc6.d/K20plexconnect -> ../init.d/plexconnect
   /etc/rc2.d/S20plexconnect -> ../init.d/plexconnect
   /etc/rc3.d/S20plexconnect -> ../init.d/plexconnect
   /etc/rc4.d/S20plexconnect -> ../init.d/plexconnect
   /etc/rc5.d/S20plexconnect -> ../init.d/plexconnect

Thoughts?  How come the service isn't starting on it's own at boot?!

Thoughts?  How come the service isn't starting on it's own at boot?!

Well it is complaining that it's missing LSB information, and by reading that wiki page it seems that it is missing the an option (eg; start).  

You can dismiss the LSB information. Its just saying that the following header is missing : 

### BEGIN INIT INFO
# Provides:          scriptname
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO

You won't really need it. It still should start to be honest. 

So if you do a 

sudo service plexconnect start

the service is starting, after rebooting its not. 

This is indeed weird, as you do not get any errors. 

Remove the services from autostart again and check if there are still files left which cause trouble.

And do a git pull to retrieve the latest data and try it again

You could assume that, however - are there any dependencies that should be up first ? eg; network, else PlexConnect will stop immediately after the start...log will tell you. The LSB info block exists for these reasons.

yea there youre right. Take a look in your log whats happening there, then we can maybe easily move on

You could assume that, however - are there any dependencies that should be up first ? eg; network, else PlexConnect will stop immediately after the start...log will tell you. The LSB info block exists for these reasons.

I renamed the existing log file (see below) just to be clear about whether anything was happening after the system is restarted.  Sure enough, after restarting the system, no new log file is created.  

17:50:45 PlexConnect: started: 17:50:45
17:50:45 PlexConnect: Version: 0.3.1+
17:50:45 PlexConnect: Python: 2.7.5 (default, Feb  6 2014, 21:23:43)
[GCC 4.4.3]
17:50:45 PlexConnect: Host OS: linux2
17:50:45 Settings: getsetting enable_plexconnect_autodetect=True
17:50:45 PlexConnect: IP_self: 192.168.0.111
17:50:45 Settings: getsetting hosttointercept=trailers.apple.com
17:50:45 Settings: getsetting port_webserver=80
17:50:45 Settings: getsetting enable_dnsserver=True
17:50:45 DNSServer: started: 17:50:45
17:50:45 Settings: getsetting port_dnsserver=53
17:50:45 Settings: getsetting ip_dnsmaster=208.122.23.23
17:50:45 Settings: getsetting prevent_atv_update=True
17:50:45 DNSServer: ***
17:50:45 DNSServer: DNSServer: Serving DNS on 192.168.0.111 port 53.
17:50:45 DNSServer: intercept: ['trailers.apple.com'] => 192.168.0.111
17:50:45 DNSServer: restrain: ['mesu.apple.com', 'appldnld.apple.com', 'appldnld.apple.com.edgesuite.net'] => 127.0.0.1
17:50:45 DNSServer: forward other to higher level DNS: 208.122.23.23
17:50:45 DNSServer: ***
17:50:45 WebServer: started: 17:50:45
17:50:45 Settings: getsetting port_webserver=80
17:50:45 Settings: getsetting enable_webserver_ssl=True
17:50:45 WebServer: started: 17:50:45
17:50:45 Settings: getsetting port_ssl=443
17:50:45 Settings: getsetting certfile=./assets/certificates/trailers.pem
17:50:45 Settings: getsetting certfile=./assets/certificates/trailers.pem
17:50:45 WebServer: ***
17:50:45 WebServer: ***
17:50:45 WebServer: WebServer: Serving HTTP on 192.168.0.111 port 80.
17:50:45 WebServer: ***
17:50:45 WebServer: WebServer: Serving HTTPS on 192.168.0.111 port 443.
17:50:45 WebServer: ***
17:50:45 ATVSettings: init class CATVSettings
17:50:45 ATVSettings: load settings
17:51:18 DNSServer: DNS request received!
17:51:18 DNSServer: Source: ('192.168.0.164', 53743)
17:51:18 DNSServer: Domain: ios-nccp-1848156627.us-west-9.elb.amazonaws.com
17:51:18 DNSServer: ***forward request
17:51:18 DNSServer: -> DNS response from higher level
17:51:55 PlexConnect: Shutting down.
17:51:55 WebServer: Shutting down.
17:51:55 WebServer: Shutting down.
17:51:55 ATVSettings: save settings
17:51:58 DNSServer: Shutting down.
17:51:58 PlexConnect: shutdown

So what are you showing us here? Is this a startup after booting from init.d or is this a manual start of PlexConnect?

Hi all, 

Im a linux noob, but I installed PMS, and then plexconnect.

I just created the symlink to PlexConnect_daemon.bash but after a reboot the service is listed in "service --status-all" but not started [ - ] 

What should I do to make the service start after a restart?

Thanks for the help, 

Regards

did you do

sudo update-rc.d plexconnect defaults

with plexconnect having your service name ?

So what are you showing us here? Is this a startup after booting from init.d or is this a manual start of PlexConnect?

What I was showing you was the logfile (created from starting using $sudo service plexconnect start). To be clear about whether anything was actually happening after the system was restarted, I renamed the existing log file (for reference) and then restarted the system.  I thought that if plexconnect was atleast trying to start, a new log file would be generated and I would see any errors right away when viewing the file.  As I mentioned, no new log file was generated after restart, which I interpret as meaning that the autostart of plexconnect does not even begin.

So if you do a 

sudo service plexconnect start

the service is starting, after rebooting its not. 

This is indeed weird, as you do not get any errors. 

 

Remove the services from autostart again and check if there are still files left which cause trouble.

And do a git pull to retrieve the latest data and try it again

I did a git pull.  I also removed the start/stop links and had a look at the various folders where things had been removed from...I don't see anything that seems out of place but then again, that doesn't mean much coming from me (findings from the various folders are below.) 

tom@HouseMedia:~$ sudo update-rc.d -f plexconnect remove
 Removing any system startup links for /etc/init.d/plexconnect ...
   /etc/rc0.d/K20plexconnect
   /etc/rc1.d/K20plexconnect
   /etc/rc2.d/S20plexconnect
   /etc/rc3.d/S20plexconnect
   /etc/rc4.d/S20plexconnect
   /etc/rc5.d/S20plexconnect
   /etc/rc6.d/K20plexconnect
tom@HouseMedia:~$ cd /etc/rc0.d
tom@HouseMedia:/etc/rc0.d$ ls
K01mediatomb            K10bacula-sd                K21spamassassin
K02apache2              K20avahi-daemon             K74bluetooth
K02hsf                  K20clamav-daemon            README
K02hylafax              K20clamav-freshclam         S01networking
K02mgetty-fax           K20dropbox                  S01unattended-upgrades
K02mt-daapd             K20postfix                  S02urandom
K02transmission-daemon  K20tightvncserver           S15wpa-ifupdown
K02webmin               K20vboxautostart-service    S20sendsigs
K02xinetd               K20vboxballoonctrl-service  S31umountnfs.sh
K05xdm                  K20vboxweb-service          S40umountfs
K08bacula-director      K20virtualbox-ose           S60umountroot
K09bacula-fd            K20yum-updatesd             S90halt
tom@HouseMedia:/etc/rc0.d$ cd /etc/rc1.d
tom@HouseMedia:/etc/rc1.d$ ls
K01mediatomb  K02mgetty-fax  K02transmission-daemon  K08bacula-director  K20clamav-daemon     K20powernowd                K20vboxweb-service  K74bluetooth  S01killprocs
K02apache2    K02mt-daapd    K02webmin               K09bacula-fd        K20clamav-freshclam  K20tightvncserver           K20virtualbox-ose   K80cups       S01pppd-dns
K02hsf        K02ntp         K02xinetd               K10bacula-sd        K20dropbox           K20vboxautostart-service    K20yum-updatesd     README        S02single
K02hylafax    K02rsync       K05xdm                  K20avahi-daemon     K20postfix           K20vboxballoonctrl-service  K21spamassassin     S01dns-clean
tom@HouseMedia:/etc/rc1.d$ cd /etc/rc2.d
tom@HouseMedia:/etc/rc2.d$ ls
README        S01transmission-daemon  S05mgetty-fax  S05xinetd       S07ondemand         S20avahi-daemon      S20fancontrol  S20tightvncserver           S20virtualbox-ose
S01dns-clean  S01unattended-upgrades  S05ntp         S06mt-daapd     S07rc.local         S20clamav-daemon     S20postfix     S20vboxautostart-service    S20yum-updatesd
S01hsf        S01xdm                  S05rsync       S07grub-common  S10powernowd.early  S20clamav-freshclam  S20powernowd   S20vboxballoonctrl-service  S25bluetooth
S01pppd-dns   S05hylafax              S05webmin      S07mediatomb    S19spamassassin     S20dropbox           S20sysstat     S20vboxweb-service          S50cups
tom@HouseMedia:/etc/rc2.d$ cd /etc/rc3.d
tom@HouseMedia:/etc/rc3.d$ ls
README        S01transmission-daemon  S05mgetty-fax  S05xinetd       S07ondemand      S20clamav-daemon     S20postfix         S20vboxautostart-service    S20yum-updatesd
S01dns-clean  S01unattended-upgrades  S05ntp         S06mt-daapd     S07rc.local      S20clamav-freshclam  S20powernowd       S20vboxballoonctrl-service  S25bluetooth
S01hsf        S01xdm                  S05rsync       S07grub-common  S19spamassassin  S20dropbox           S20sysstat         S20vboxweb-service          S50cups
S01pppd-dns   S05hylafax              S05webmin      S07mediatomb    S20avahi-daemon  S20fancontrol        S20tightvncserver  S20virtualbox-ose
tom@HouseMedia:/etc/rc3.d$ cd /etc/rc4.d
tom@HouseMedia:/etc/rc4.d$ ls
README        S01transmission-daemon  S05ntp       S07grub-common  S19spamassassin      S20dropbox     S20sysstat                  S20vboxweb-service  S50cups
S01dns-clean  S01xdm                  S05rsync     S07mediatomb    S20avahi-daemon      S20fancontrol  S20tightvncserver           S20virtualbox-ose
S01hsf        S05hylafax              S05xinetd    S07ondemand     S20clamav-daemon     S20postfix     S20vboxautostart-service    S20yum-updatesd
S01pppd-dns   S05mgetty-fax           S06mt-daapd  S07rc.local     S20clamav-freshclam  S20powernowd   S20vboxballoonctrl-service  S25bluetooth
tom@HouseMedia:/etc/rc4.d$ cd /etc/rc5.d
tom@HouseMedia:/etc/rc5.d$ ls
README        S01transmission-daemon  S05mgetty-fax  S05xinetd       S07ondemand      S20clamav-daemon     S20postfix         S20vboxautostart-service    S20yum-updatesd
S01dns-clean  S01unattended-upgrades  S05ntp         S06mt-daapd     S07rc.local      S20clamav-freshclam  S20powernowd       S20vboxballoonctrl-service  S25bluetooth
S01hsf        S01xdm                  S05rsync       S07grub-common  S19spamassassin  S20dropbox           S20sysstat         S20vboxweb-service          S50cups
S01pppd-dns   S05hylafax              S05webmin      S07mediatomb    S20avahi-daemon  S20fancontrol        S20tightvncserver  S20virtualbox-ose
tom@HouseMedia:/etc/rc5.d$ cd /etc/rc6.d
tom@HouseMedia:/etc/rc6.d$ ls
K01mediatomb  K02mgetty-fax           K02xinetd           K10bacula-sd         K20dropbox                K20vboxballoonctrl-service  K21spamassassin  S01unattended-upgrades  S31umountnfs.sh
K02apache2    K02mt-daapd             K05xdm              K20avahi-daemon      K20postfix                K20vboxweb-service          K74bluetooth     S02urandom              S40umountfs
K02hsf        K02transmission-daemon  K08bacula-director  K20clamav-daemon     K20tightvncserver         K20virtualbox-ose           README           S15wpa-ifupdown         S60umountroot
K02hylafax    K02webmin               K09bacula-fd        K20clamav-freshclam  K20vboxautostart-service  K20yum-updatesd             S01networking    S20sendsigs             S90reboot

 I then added the LSB Script comments into the PlexConnect_daemon.bash file and did the update-rc.d

### BEGIN INIT INFO
# Provides:          scriptname
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO
tom@HouseMedia:~/PlexConnect-master$ sudo update-rc.d plexconnect defaults
 Adding system startup for /etc/init.d/plexconnect ...
   /etc/rc0.d/K20plexconnect -> ../init.d/plexconnect
   /etc/rc1.d/K20plexconnect -> ../init.d/plexconnect
   /etc/rc6.d/K20plexconnect -> ../init.d/plexconnect
   /etc/rc2.d/S20plexconnect -> ../init.d/plexconnect
   /etc/rc3.d/S20plexconnect -> ../init.d/plexconnect
   /etc/rc4.d/S20plexconnect -> ../init.d/plexconnect
   /etc/rc5.d/S20plexconnect -> ../init.d/plexconnect

it should be noted that I have to use sudo to have any sort of meaningful interaction with plexconnect at the command line (see below.) Perhaps this is somehow related to the problem of the plexconnect service not auto-starting?   

tom@HouseMedia:~$ service plexconnect start
Starting PlexConnect...
tom@HouseMedia:~$ service plexconnect status
PlexConnect is not running
tom@HouseMedia:~$ sudo service plexconnect start
[sudo] password for tom:
Starting PlexConnect...
tom@HouseMedia:~$ sudo service plexconnect status
PlexConnect is running
tom@HouseMedia:~$ service plexconnect status
cat: /var/PlexConnect_daemon.pid: Permission denied
rm: cannot remove `/var/PlexConnect_daemon.pid': Permission denied
PlexConnect is not running
 

No, this is indeed just typical behaviour. Dont worry about it not working without sudo.

So you basically have no PlexConnect.log anymore in the PlexConnect folder?

If not, change the logpath to /vat/log/ and restart plexconnect via services. See if the log appears there. If it does, do not delete it, stop it, and restart it and see if something comes up. Without any logs from plexconnect why its stopping after booting (nor not even starting properly) theres not much we can do right now

So you basically have no PlexConnect.log anymore in the PlexConnect folder?

If not, change the logpath to /vat/log/ and restart plexconnect via services. See if the log appears there. If it does, do not delete it, stop it, and restart it and see if something comes up. Without any logs from plexconnect why its stopping after booting (nor not even starting properly) theres not much we can do right now

Well, I currently have a PlexConnect.log  in the PlexConnect folder, but that is because I have manually started the service since then I wrote that reply post. 

I tried changing the logpath within Settings.cfg to write to /var/log and restarted the system...there's no /var/log/PlexConnect.log created either and PlexConnect has not started.

Since I have no idea what an upstart script is supposed to look like, I did some googling.

I found this very simple example (see below) and I noticed there are "start on runlevel [...]" commands that are no where to be found in PlexConnect_daemon.bash. And now I'm wondering if I misunderstood what I was being asked to do earlier or what is happening at all.

Currently, /etc/init.d/plexconnect is symlinked to ~PlexConnect-master/PlexConnect_daemon.bash.  So when I use $sudo service plexconnect start  , my understanding is it looks inside the /etc/init.d folder for something named 'plexconnect', which is currently pointed at ~/PlexConnect-master/PlexConnect_daemon.bash.  Looking at the .bash file, it looks like that when the augment 'start' is input,  the script provides instructions for what to do to actually start PlexConnect.  There's also instructions for how to handle "stop" and "restart" augments as well.

So where in the .bash file are there conditions for starting (such as "start on runlevel [2345]) PlexConnect and the actually command that is used to start it? 

# testservice - test service job file

description “my service description”
author “Me myself@i.com

Stanzas

Stanzas control when and how a process is started and stopped

See a list of stanzas here: http://upstart.ubuntu.com/wiki/Stanzas#respawn

When to start the service

start on runlevel [2345]

When to stop the service

stop on runlevel [016]

Automatically restart process if crashed

respawn

Essentially lets upstart know the process will detach itself to the background

This option does not seem to be of great importance, so it does not need to be set.

#expect fork

Specify working directory

chdir /home/user/testcode

Specify the process/command to start, e.g.

exec python mycommand.py arg1 arg2

yes thats correct. But there can't be anything wrong with this as you said 

sudo service plexconnect start

does start plexconnect, doesn't it ?

So there is just something wrong with your upstart. 

So if you change the log path back to .

does it create a log when booting does it not?

If it doesn't, then we just know its not about plexconnect at all but about your upstart scripts

EDIT: Fixed!  I had to disable Roaming Mode [Linux Mint 16 in this case] on my ethernet interface, which is enabled by default.  Presumably the interface wasn't being made available to plexconnect in some fashion.  Now, having turned Roaming Mode off, the plexconnect service starts properly at boot.  Hope this helps someone else!

Hi guys,

Linux Mint 16.

Unpacked PlexConnect-master into /home

Configured the keys, put them into the assets folder, ran ./PlexConnect.py, configured the appletv3, all works well.  What a fantastic little program!

Now, I'm trying to get it to start automatically using the upstart service and PlexConnect_daemon.bash

I did the following:

ln -s /home/PlexConnect-master/PlexConnect_daemon.bash /etc/init.d/plexconnect

update-rc.d plexconnect defaults

and the following works as expected:

# service plexconnect start
Starting PlexConnect...
# service plexconnect status
PlexConnect is running
# service plexconnect stop
Stopping PlexConnect...
 

Problem is, however, that this doesn't start at boot.  I have to log in and run 'service plexconnect start'.

Any tips or ideas would be welcome.

The maddening thing is, I got this working in a caffeine-induced frenzy a few weeks back, failed to write down what I did, and now I've reinstalled my system and can't replicate it.  Argh :P

Thanks for hanging in with me! Still not working....

...

So there is just something wrong with your upstart. 

So if you change the log path back to .

does it create a log when booting does it not?

If it doesn't, then we just know its not about plexconnect at all but about your upstart scripts

I can start the service using the command you mentioned yes; the issue is that it does not start at boot automatically. I'm not sure what's wrong with this particular upstart job considering there are many others in there and many of them automatically start themselves.

I noticed that similar complaints about upstart jobs not starting can be solved by using an "pre-script" to delay running the problematic upstart job until other services have started first.  I'd like to try that out but can someone refer me to a clear explanation of how to use "pre-scripting" to create that delay? Is it simply another upstart job that points to another upstart job after executing?  

EDIT: Fixed!  I had to disable Roaming Mode [Linux Mint 16 in this case] on my ethernet interface, which is enabled by default.  Presumably the interface wasn't being made available to plexconnect in some fashion.  Now, having turned Roaming Mode off, the plexconnect service starts properly at boot.  Hope this helps someone else!

After much googling, I think you're referring to the wifi roaming that Network-Manager provides?  I disabled it's upstart job (an example of an upstart job that automatically starts) and restarted the system and plexconnect still does not start on it's own.  As a reminder, nothing is added to the existing plexconnect log at boot, nor is a log file created at boot if I set the logpath to another location.  The upstart job is definitely not starting.  

To be honest we are just running out of options. If its not trying to start the python script when booting this definitely has nothing to do with PlexConnect, theres just something completely wrong with upstarts on your server side :P

Without having to reread your old posts, what exact linux version are you running?

Try putting in a pause (1 to 5) before the code that detects if the network interface is up on OSX.

A pause of 1 needs to be put in on some hackintoshes but we still don't know why yet, maybe some Linux users have the same issue.

Try putting in a pause (1 to 5) before the code that detects if the network interface is up on OSX.

A pause of 1 needs to be put in on some hackintoshes but we still don't know why yet, maybe some Linux users have the same issue.

I inserted a "sleep 5" at the start of the section involving OSX and rebooted; no luck.

To be honest we are just running out of options. If its not trying to start the python script when booting this definitely has nothing to do with PlexConnect, theres just something completely wrong with upstarts on your server side :P

Without having to reread your old posts, what exact linux version are you running?

Yeah, I thought that would be the case by now.  I don't understand when this script wouldn't start but others do.  Using Ubuntu Linux 10.04.2

Just to be clear, this is the contents of the upstart script:

#!/bin/bash

Linux PlexConnect start stop script

BEGIN INIT INFO

Provides: scriptname

Required-Start: $remote_fs $syslog

Required-Stop: $remote_fs $syslog

Default-Start: 2 3 4 5

Default-Stop: 0 1 6

Short-Description: Start daemon at boot time

Description: Enable service provided by daemon.

END INIT INFO

Package

DNAME=“PlexConnect”
PNAME=“PlexConnect_daemon”

Others

current path resolver from http://stackoverflow.com/a/246128

SOURCE=“${BASH_SOURCE[0]}”
while [ -h “$SOURCE” ]; do
DIR=“$( cd -P “$( dirname “$SOURCE” )” && pwd )”
SOURCE=“$(readlink “$SOURCE”)”
[[ $SOURCE != /* ]] && SOURCE=“$DIR/$SOURCE”
done
INSTALL_DIR=“$( cd -P “$( dirname “$SOURCE” )” && pwd )”

PYTHON=“python2.7”
PROGRAM=“${INSTALL_DIR}/${PNAME}.py”
PID_FILE=“/var/${PNAME}.pid”

start_daemon ()
{
cd “${INSTALL_DIR}”
${PYTHON} ${PROGRAM} --pidfile ${PID_FILE}
}

stop_daemon ()
{
kill cat ${PID_FILE}
wait_for_status 1 20 || kill -9 cat ${PID_FILE}
rm -f ${PID_FILE}
}

daemon_status ()
{
if [ -f ${PID_FILE} ] && kill -0 cat ${PID_FILE} > /dev/null 2>&1; then
return
fi
rm -f ${PID_FILE}
return 1
}

wait_for_status ()
{
counter=$2
while [ ${counter} -gt 0 ]; do
daemon_status
[ $? -eq $1 ] && return
let counter=counter-1
sleep 1
done
return 1
}

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

Currently supports only OSX “Darwin” OS

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
if [[ “$OSTYPE” == “darwin”* ]]; then
CheckForNetwork
while [ “${NETWORKUP}” != “-YES-” ]
do
sleep 5
NETWORKUP=
CheckForNetwork
done
fi

Now do what you need to do.

case $1 in
start)
if daemon_status; then
echo ${DNAME} is already running
else
echo Starting ${DNAME}…
start_daemon
fi
;;
stop)
if daemon_status; then
echo Stopping ${DNAME}…
stop_daemon
else
echo ${DNAME} is not running
fi
;;
status)
if daemon_status; then
echo ${DNAME} is running
exit 0
else
echo ${DNAME} is not running
exit 1
fi
;;
restart)
$0 stop && sleep 2 && $0 start && sleep 2 && $0 status
;;
*)
exit 1
;;
esac