Ubuntu 12.04 init script?

Does anyone have one they can share?

I have PlexConnect running on boot, but it's not the most elegant solution:

/etc/init/plexconnect.conf

# plexconnect - service job file

description “PlexConnect”
author “http://www.plexapp.com/

Start the media server after network and filesystem

Otherwise this lead to a unresponsive server

start on filesystem and net-device-up IFACE!=lo

When to stop the service

stop on runlevel [016]

Automatically restart process if crashed

respawn

Sets nice and ionice level for job

nice -5

What to execute

script
sleep 10
start-stop-daemon --start --exec /usr/sbin/start_plexconnect
end script

I added the "sleep 10" to ensure that PMS had started so PlexConnect would find it. I bet there's a better way to do that, but I couldn't find it.

/usr/sbin/start_plexconnect

#!/bin/bash

PLEXCONNECT_SRC=/home/john/src/PlexConnect
PLEXCONNECT_LOG=/tmp/plexconnect.log

i=0
while [ -r “$PLEXCONNECT_LOG-$i” ]; do
(( i++ ))
done

(cd $PLEXCONNECT_SRC; ./PlexConnect.py > $PLEXCONNECT_LOG-$i)

You'll obviously have to change PLEXCONNECT_SRC to reflect where you have your PlexConnect source folder located.

Lastly I had to comment out the following lines from PlexConnect.py to prevent it from exiting imediatly:

    try:
        key = raw_input()
    except KeyboardInterrupt:
        dprint('PlexConnect', 0, "^C received.")
finally:
    dprint('PlexConnect', 0,  "Shutting down.")
    cmd_DNSServer.put('shutdown')
    cmd_WebServer.put('shutdown')

    p_DNSServer.join()
    p_WebServer.join()

This has been working for me, but I'd love to hear if someone else has come up with a cleaner solution

I have PlexConnect running on boot, but it's not the most elegant solution:

/etc/init/plexconnect.conf

# plexconnect - service job file

description “PlexConnect”
author “http://www.plexapp.com/

Start the media server after network and filesystem

Otherwise this lead to a unresponsive server

start on filesystem and net-device-up IFACE!=lo

When to stop the service

stop on runlevel [016]

Automatically restart process if crashed

respawn

Sets nice and ionice level for job

nice -5

What to execute

script
sleep 10
start-stop-daemon --start --exec /usr/sbin/start_plexconnect
end script

I added the "sleep 10" to ensure that PMS had started so PlexConnect would find it. I bet there's a better way to do that, but I couldn't find it.

/usr/sbin/start_plexconnect

#!/bin/bash

PLEXCONNECT_SRC=/home/john/src/PlexConnect
PLEXCONNECT_LOG=/tmp/plexconnect.log

i=0
while [ -r “$PLEXCONNECT_LOG-$i” ]; do
(( i++ ))
done

(cd $PLEXCONNECT_SRC; ./PlexConnect.py > $PLEXCONNECT_LOG-$i)

You'll obviously have to change PLEXCONNECT_SRC to reflect where you have your PlexConnect source folder located.

Lastly I had to comment out the following lines from PlexConnect.py to prevent it from exiting imediatly:

    try:
        key = raw_input()
    except KeyboardInterrupt:
        dprint('PlexConnect', 0, "^C received.")
finally:
    dprint('PlexConnect', 0,  "Shutting down.")
    cmd_DNSServer.put('shutdown')
    cmd_WebServer.put('shutdown')

    p_DNSServer.join()
    p_WebServer.join()

This has been working for me, but I'd love to hear if someone else has come up with a cleaner solution

Hey there. how did you get this to work ? i added the files and added chmod +x to the sbin file. but still can't get it to boot noir useing the sudo service command

Hey there. how did you get this to work ? i added the files and added chmod +x to the sbin file. but still can't get it to boot noir useing the sudo service command

Is there any kind of error you're getting?

Maybe double check that line #3 in start_plexconnect is pointing to the correct place on your computer.

Getting the following when trying to run script cant work out what is going on: 

@PlexMediaCenter:/usr/sbin# ./start_plexconnect
./start_plexconnect: line 3: Support/PlexConnect/: No such file or directory
./start_plexconnect: line 11: ./PlexConnect.py: No such file or directory
 
Script output below: 
 
#!/bin/bash
 
PLEXCONNECT_SRC=/home/alancarslaw/Library/Application Support/PlexConnect
PLEXCONNECT_LOG=/tmp/plexconnect.log
 
i=0
while [ -r "$PLEXCONNECT_LOG-$i" ]; do
  (( i++ ))
done
 
(cd $PLEXCONNECT_SRC; ./PlexConnect.py > $PLEXCONNECT_LOG-$i)
 
 
PlexConnect is located in the following: 
 
root@PlexMediaCenter:/home/alancarslaw/Library/Application Support/PlexConnect# ls -l
total 144
drwxr-xr-x 4 root root  4096 Jun 11 20:10 assets
-rw-r--r-- 1 root root   162 Jun 11 22:31 ATVSettings.cfg
-rwxr-xr-x 1 root root  3608 Jun 11 11:35 ATVSettings.py
-rw-r--r-- 1 root root  4332 Jun 11 21:52 ATVSettings.pyc
-rwxr-xr-x 1 root root  1372 Jun 11 11:35 Debug.py
-rw-r--r-- 1 root root  1435 Jun 11 21:52 Debug.pyc
-rwxr-xr-x 1 root root  7264 Jun 11 11:35 DNSServer.py
-rw-r--r-- 1 root root  3913 Jun 11 21:52 DNSServer.pyc
-rwxr-xr-x 1 root root  2578 Jun 11 11:35 PlexConnect.py
-rwxr-xr-x 1 root root  3653 Jun 11 11:35 PlexGDM.py
-rw-r--r-- 1 root root  3171 Jun 11 21:52 PlexGDM.pyc
-rwxr-xr-x 1 root root  2870 Jun 11 11:35 README.md
-rw-r--r-- 1 root root   126 Jun 11 22:06 Settings.cfg
-rwxr-xr-x 1 root root  2482 Jun 11 20:17 Settings.py
-rw-r--r-- 1 root root  3105 Jun 11 21:52 Settings.pyc
-rwxr-xr-x 1 root root  7887 Jun 11 11:35 WebServer.py
-rw-r--r-- 1 root root  5641 Jun 11 21:52 WebServer.pyc
-rwxr-xr-x 1 root root 31629 Jun 11 11:35 XMLConverter.py
-rw-r--r-- 1 root root 27728 Jun 11 21:52 XMLConverter.pyc
root@PlexMediaCenter:/home/alancarslaw/Library/Application Support/PlexConnect# 

I think your problem is with this line:

PLEXCONNECT_SRC=/home/alancarslaw/Library/Application Support/PlexConnect

The space in "Application Support" is probably causing problems.

Solution 1:

Change it to /home/alancarslaw/Library/Application\ Support/PlexConnect

Note the \ between Application and Support

Solution 2:

Change the line to PLEXCONNECT_SRC="/home/alancarslaw/Library/Application Support/PlexConnect"

Note the quotation marks

Solution 3:

Change the last line to (cd "$PLEXCONNECT_SRC"; ./PlexConnect.py > $PLEXCONNECT_LOG-$i)

Note the quotation marks

Any of those will probably fix the problem.

Let me know how it turns out.

Hi, 

Yes Solution 2 and 3 appear to have fixed the issue however when I run the script manually it does start the required Plexconnect Services but does not go back to the command prompt, any ideas why its not not starting and returning back to the command prompt to allow me enter other commands? 

root@PlexMediaCenter:/usr/sbin# ./start_plexconnect

I am having to ctrl c which is then stopping the services :( 

It’s not designed to.


If you want to start/stop the service you need to use the upstart syntax.

‘sudo start plexconnect’ or ‘sudo stop plexconnect’

Any idea how I could go about setting this up? 

would appreciate your help/guidance on this - plus would be beneficial to get this working for all Ubuntu users because as you know most services now are using upstart 

Anyone, able to advise how you can bring this in as an upstart? interested to hear from you 

This is using upstart.

The /etc/init/plexconnect.conf is the upstart script and /usr/sbin/start_plexconnect is just a helper script I wrote because that's how PMS did it and I was lazy.

'sudo start plexconnect' should work if both of those files are in place.

Hope that helps :)

It hasn't been merged (and now would cause some conflicts if attempted automatically), but you can see how to start the script cleanly as a daemon and return to the command line here: https://github.com/iBaa/PlexConnect/pull/35

If you kill the parent process (the one with the lowest PID) then it will also clean up the children (the DNS server and the HTTP server).

Hope this is helpful.

Thanks how do you add this to upstart though I have the files you created, work and are in right location just unsure about upstart how to configure so that when system reboots plexconnect will start automatically.

If you have those files in place it should be working on boot.


Is it not?


Don’t forget about the 10 second sleep when you’re checking to see if it worked.

This may help. I put my init script in that post. I have not tested in Ubuntu and I put some specific notes for non RHEL based distro's (I am using Scientific Linux) in the post just before the init script itself.

http://forums.plexapp.com/index.php/topic/71220-linux-install-for-plex-connect/?p=411553

@wilsoncd2 are you able to modify your script for me so it’s Ubuntu compatible… I will the test this out as in which case will post results… If works we can then publish this on the forum for others to use… I am desperate to get this running as a daemon due to being fed up plexconnect disconnecting when close session :frowning:

@wilsoncd2 are you able to modify your script for me so it's Ubuntu compatible... I will the test this out as in which case will post results... If works we can then publish this on the forum for others to use.... I am desperate to get this running as a daemon due to being fed up plexconnect disconnecting when close session :(

Unfortunately I do not have a Unbuntu system to use to modify the script on. I can make a guess from memory but that is as good as I can do right now. Try this modified version. But remember you must have the application 'screen' installed for this script to work. Also please check and modify (if neccessary) the following.

  1. the import  statement ". /lib/lsb/init-functions" (make sure that /lib/lsb/init-functions is a file that exists). Otherwise you will need to correct the path to adjust for ubuntus location. 
  2. DAEMON_OPTS="-S PlexConnect -d -m /var/lib/PlexConnect/PlexConnect.py" (make sure the path is correct to your PlexConnect.py, or update it to the correct path).

I have modified this from memory so it is very likely that it will not work. Put this in to the file /etc/init.d/plexconnect and see if it works.

#!/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 /var/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=“$?”
#pidofproc -p $PIDFILE > /dev/null 2>&1

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.

#log_daemon_msg “Starting the process” “$NAME”

Start the daemon with the help of start-stop-daemon

Log the message appropriately

if start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON $DAEMON_OPTS; then
#if start_daemon -u $DAEMON_USER -p $PIDFILE $DAEMON $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=“$?”
#pidofproc -p $PIDFILE > /dev/null 2>&1
if [ “$?” = 0 ]; then
start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
#killproc -p $PIDFILE
/bin/rm -rf $PIDFILE
log_success_msg ““Stopping the $NAME process””
fi
else
log_failure_msg “$NAME process is not running”
#log_end_msg 0
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 $?
#pidofproc -p $PIDFILE > /dev/null 2>&1
log_success_msg “$NAME process is running”
else
log_failure_msg “$NAME process is not running”
#log_end_msg 0
fi
;;
reload)
$0 restart
;;
*)

For invalid arguments, print the usage message.

echo “Usage: $0 {start|stop|restart|reload|status}”
exit 2
;;
esac

@wilsoncd2 are you able to modify your script for me so it's Ubuntu compatible... I will the test this out as in which case will post results... If works we can then publish this on the forum for others to use.... I am desperate to get this running as a daemon due to being fed up plexconnect disconnecting when close session :(

Did my scripts not work for you?

Are you getting any kind of message in the log file (default /tmp/plexconnect-0.log)?