# Ubuntu 12.04 init script?

**URL:** <https://forums.plex.tv/t/ubuntu-12-04-init-script/35663>\
**Category:** Apps & Creations\
**Created:** [June 7, 2013, 6:49am UTC](https://forums.plex.tv/t/ubuntu-12-04-init-script/35663 "2013-06-07T06:49:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maxwell\_Smart](https://avatars.discourse-cdn.com/v4/letter/m/b9bd4f/32.png) [@Maxwell\_Smart](https://forums.plex.tv/u/Maxwell_Smart)\
**Post date:** [June 7, 2013, 6:49am UTC](https://forums.plex.tv/t/ubuntu-12-04-init-script/35663/1 "2013-06-07T06:49:59Z")

</div>

Does anyone have one they can share?

---

<div class="post-metadata">

**Author:** ![tallerthenyou](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/tallerthenyou/32/102571_2.png) [@tallerthenyou](https://forums.plex.tv/u/tallerthenyou)\
**Post date:** [June 7, 2013, 4:57pm UTC](https://forums.plex.tv/t/ubuntu-12-04-init-script/35663/2 "2013-06-07T16:57:56Z")

</div>

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/](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

---

<div class="post-metadata">

**Author:** ![Xmagixx](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/xmagixx/32/86758_2.png) [@Xmagixx](https://forums.plex.tv/u/Xmagixx)\
**Post date:** [June 10, 2013, 11:02am UTC](https://forums.plex.tv/t/ubuntu-12-04-init-script/35663/3 "2013-06-10T11:02:22Z")

</div>

> 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/](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

---

<div class="post-metadata">

**Author:** ![tallerthenyou](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/tallerthenyou/32/102571_2.png) [@tallerthenyou](https://forums.plex.tv/u/tallerthenyou)\
**Post date:** [June 10, 2013, 3:26pm UTC](https://forums.plex.tv/t/ubuntu-12-04-init-script/35663/4 "2013-06-10T15:26:33Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![Alan.Carslaw](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@Alan.Carslaw](https://forums.plex.tv/u/Alan.Carslaw)\
**Post date:** [June 11, 2013, 9:33pm UTC](https://forums.plex.tv/t/ubuntu-12-04-init-script/35663/5 "2013-06-11T21:33:22Z")

</div>

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

@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

&nbsp;

Script output below:&nbsp;

&nbsp;

#!/bin/bash

&nbsp;

PLEXCONNECT\_SRC=/home/alancarslaw/Library/Application Support/PlexConnect

PLEXCONNECT\_LOG=/tmp/plexconnect.log

&nbsp;

i=0

while [-r "$PLEXCONNECT\_LOG-$i"]; do

&nbsp; (( i++ ))

done

&nbsp;

(cd $PLEXCONNECT\_SRC; ./PlexConnect.py \> $PLEXCONNECT\_LOG-$i)

&nbsp;

&nbsp;

PlexConnect is located in the following:&nbsp;

&nbsp;

root@PlexMediaCenter:/home/alancarslaw/Library/Application Support/PlexConnect# ls -l

total 144

drwxr-xr-x 4 root root &nbsp;4096 Jun 11 20:10 assets

-rw-r--r-- 1 root root &nbsp; 162 Jun 11 22:31 ATVSettings.cfg

-rwxr-xr-x 1 root root &nbsp;3608 Jun 11 11:35 ATVSettings.py

-rw-r--r-- 1 root root &nbsp;4332 Jun 11 21:52 ATVSettings.pyc

-rwxr-xr-x 1 root root &nbsp;1372 Jun 11 11:35 Debug.py

-rw-r--r-- 1 root root &nbsp;1435 Jun 11 21:52 Debug.pyc

-rwxr-xr-x 1 root root &nbsp;7264 Jun 11 11:35 DNSServer.py

-rw-r--r-- 1 root root &nbsp;3913 Jun 11 21:52 DNSServer.pyc

-rwxr-xr-x 1 root root &nbsp;2578 Jun 11 11:35 PlexConnect.py

-rwxr-xr-x 1 root root &nbsp;3653 Jun 11 11:35 PlexGDM.py

-rw-r--r-- 1 root root &nbsp;3171 Jun 11 21:52 PlexGDM.pyc

-rwxr-xr-x 1 root root &nbsp;2870 Jun 11 11:35 README.md

-rw-r--r-- 1 root root &nbsp; 126 Jun 11 22:06 Settings.cfg

-rwxr-xr-x 1 root root &nbsp;2482 Jun 11 20:17 Settings.py

-rw-r--r-- 1 root root &nbsp;3105 Jun 11 21:52 Settings.pyc

-rwxr-xr-x 1 root root &nbsp;7887 Jun 11 11:35 WebServer.py

-rw-r--r-- 1 root root &nbsp;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#&nbsp;

---

<div class="post-metadata">

**Author:** ![tallerthenyou](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/tallerthenyou/32/102571_2.png) [@tallerthenyou](https://forums.plex.tv/u/tallerthenyou)\
**Post date:** [June 11, 2013, 11:18pm UTC](https://forums.plex.tv/t/ubuntu-12-04-init-script/35663/6 "2013-06-11T23:18:40Z")

</div>

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&nbsp;/home/alancarslaw/Library/Application\ Support/PlexConnect

Note the \ between Application and Support

Solution 2:

Change the line to&nbsp;PLEXCONNECT\_SRC="/home/alancarslaw/Library/Application Support/PlexConnect"

Note the quotation marks

Solution 3:

Change the last line to&nbsp;(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.

---

<div class="post-metadata">

**Author:** ![Alan.Carslaw](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@Alan.Carslaw](https://forums.plex.tv/u/Alan.Carslaw)\
**Post date:** [June 12, 2013, 5:44pm UTC](https://forums.plex.tv/t/ubuntu-12-04-init-script/35663/7 "2013-06-12T17:44:45Z")

</div>

Hi,&nbsp;

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?&nbsp;

root@PlexMediaCenter:/usr/sbin# ./start\_plexconnect

---

<div class="post-metadata">

**Author:** ![Alan.Carslaw](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@Alan.Carslaw](https://forums.plex.tv/u/Alan.Carslaw)\
**Post date:** [June 12, 2013, 5:45pm UTC](https://forums.plex.tv/t/ubuntu-12-04-init-script/35663/8 "2013-06-12T17:45:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tallerthenyou](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/tallerthenyou/32/102571_2.png) [@tallerthenyou](https://forums.plex.tv/u/tallerthenyou)\
**Post date:** [June 12, 2013, 5:53pm UTC](https://forums.plex.tv/t/ubuntu-12-04-init-script/35663/9 "2013-06-12T17:53:11Z")

</div>

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’

---

<div class="post-metadata">

**Author:** ![Alan.Carslaw](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@Alan.Carslaw](https://forums.plex.tv/u/Alan.Carslaw)\
**Post date:** [June 12, 2013, 5:58pm UTC](https://forums.plex.tv/t/ubuntu-12-04-init-script/35663/10 "2013-06-12T17:58:44Z")

</div>

Any idea how I could go about setting this up?&nbsp;

---

<div class="post-metadata">

**Author:** ![Alan.Carslaw](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@Alan.Carslaw](https://forums.plex.tv/u/Alan.Carslaw)\
**Post date:** [June 12, 2013, 5:59pm UTC](https://forums.plex.tv/t/ubuntu-12-04-init-script/35663/11 "2013-06-12T17:59:43Z")

</div>

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&nbsp;

---

<div class="post-metadata">

**Author:** ![Alan.Carslaw](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@Alan.Carslaw](https://forums.plex.tv/u/Alan.Carslaw)\
**Post date:** [June 12, 2013, 9:39pm UTC](https://forums.plex.tv/t/ubuntu-12-04-init-script/35663/12 "2013-06-12T21:39:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tallerthenyou](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/tallerthenyou/32/102571_2.png) [@tallerthenyou](https://forums.plex.tv/u/tallerthenyou)\
**Post date:** [June 12, 2013, 11:14pm UTC](https://forums.plex.tv/t/ubuntu-12-04-init-script/35663/13 "2013-06-12T23:14:56Z")

</div>

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 :)

---

<div class="post-metadata">

**Author:** ![mmccurdy](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/mmccurdy/32/83290_2.png) [@mmccurdy](https://forums.plex.tv/u/mmccurdy)\
**Post date:** [June 13, 2013, 5:25am UTC](https://forums.plex.tv/t/ubuntu-12-04-init-script/35663/14 "2013-06-13T05:25:43Z")

</div>

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:&nbsp;[https://github.com/iBaa/PlexConnect/pull/35](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.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/plex/original/3X/2/a/2acb9765406f63293d357b4ec509ec39aa28f2ad.png) [@system](https://forums.plex.tv/u/system)\
**Post date:** [June 13, 2013, 5:40am UTC](https://forums.plex.tv/t/ubuntu-12-04-init-script/35663/15 "2013-06-13T05:40:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tallerthenyou](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/tallerthenyou/32/102571_2.png) [@tallerthenyou](https://forums.plex.tv/u/tallerthenyou)\
**Post date:** [June 13, 2013, 1:55pm UTC](https://forums.plex.tv/t/ubuntu-12-04-init-script/35663/16 "2013-06-13T13:55:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![wilsoncd2](https://avatars.discourse-cdn.com/v4/letter/w/ce73a5/32.png) [@wilsoncd2](https://forums.plex.tv/u/wilsoncd2)\
**Post date:** [June 14, 2013, 12:48am UTC](https://forums.plex.tv/t/ubuntu-12-04-init-script/35663/17 "2013-06-14T00:48:44Z")

</div>

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](http://forums.plexapp.com/index.php/topic/71220-linux-install-for-plex-connect/?p=411553)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/plex/original/3X/2/a/2acb9765406f63293d357b4ec509ec39aa28f2ad.png) [@system](https://forums.plex.tv/u/system)\
**Post date:** [June 14, 2013, 6:45am UTC](https://forums.plex.tv/t/ubuntu-12-04-init-script/35663/18 "2013-06-14T06:45:04Z")

</div>

@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 ☹

---

<div class="post-metadata">

**Author:** ![wilsoncd2](https://avatars.discourse-cdn.com/v4/letter/w/ce73a5/32.png) [@wilsoncd2](https://forums.plex.tv/u/wilsoncd2)\
**Post date:** [June 14, 2013, 7:38am UTC](https://forums.plex.tv/t/ubuntu-12-04-init-script/35663/19 "2013-06-14T07:38:47Z")

</div>

> @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 &nbsp;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.&nbsp;
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
```

---

<div class="post-metadata">

**Author:** ![tallerthenyou](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/tallerthenyou/32/102571_2.png) [@tallerthenyou](https://forums.plex.tv/u/tallerthenyou)\
**Post date:** [June 14, 2013, 2:17pm UTC](https://forums.plex.tv/t/ubuntu-12-04-init-script/35663/20 "2013-06-14T14:17:06Z")

</div>

> @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)?

[Next page](https://forums.plex.tv/t/ubuntu-12-04-init-script/35663.md?page=2)
