Preliminary instructions to run daemon on OSX

please move the com.plex.plexconnect.plist file in  /Library/LaunchDaemons/  

Now it is running but lan and Plex are not up an running (DNS was bind to 127.0.0.1). I guess a sleep in PlexConnect.py would help.

ADD: adding a sleep and starting PlexConnect.py from plist does it.

I've been playing with it and here is what I have so far:

save this as /Library/LaunchDaemons/com.plex.plexconnect.plist (replace  2 times with correct path)

I've tried your plist, it can start PlexConnect correctly but I can't stop it, which is necessary once in a while to do an update. 

Anybody?

I've tried your plist, it can start PlexConnect correctly but I can't stop it, which is necessary once in a while to do an update. 

Anybody?

This plist in /Library/LaunchDaemons does the job for me:

<?xml version="1.0" encoding="UTF-8"?>



	KeepAlive
	
	Label
	com.plex.plexconnect
	OnDemand
	
	ProgramArguments
	
		/Applications/PlexConnect-master/PlexConnect.py
	
	RunAtLoad
	
	ServiceDescription
	PlexConnect
	UserName
	root
	WorkingDirectory
	/Applications/PlexConnect-master/


And putting a time.sleep(20) in the startup() function of PlexConnect.py is also required because launchctl runs the PlexConnect.py to early.

Note: you must adapt the path to your PlexConnect folder if you try this plist.

...

            python
            /PlexConnect_daemon.py

python is redundant and also using a demon script - launchctl is already running in background - why not use PlexConnect.py directly?

Not trying to say anyones plist is right or wrong but cant you guys use this one this guy created? I belive its simplified a little bit more in my opinion. Obviously you would have to change some setting/directories but it should have the same effect I would imagine if anyone wants to test with it. Either way hope one of these plists can work out for you guys.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"<a data-ipb='nomediaparse'  class="bbc_url" href='http://www.apple.com/DTDs/PropertyList-1.0.dtd'>http://www.apple.com/DTDs/PropertyList-1.0.dtd</a>"><br>





Label

com.plex.plexconnect

WorkingDirectory

/Users/chris/PlexConnect

ProgramArguments



/Users/chris/PlexConnect/PlexConnect_daemon.py



RunAtLoad



KeepAlive








Also I still use an applescript to load plexconnect and it works everytime for me. You guys know you must be logged in for pms to load also you cannot log out it will shutdown pms. I use lockscreen or fastuser to keep pms running 24/7.


If you guys need to add a timeout to plexconnect.py I belive you are having the same issue I was with trying to run plexconnect as a plist. Launchctl always seemed very unreliable for me. I still use applescript without any issues here is the code from another post. From what I understand this code kills the process and will work with any version of plexconnect now and in the future. I think you guys are beating yourselves up trying to get this to work with launchctl, i know I did for days until I discovered this post #97:http://forums.plexapp.com/index.php/topic/70149-has-anyone-created-an-applescriptapplication-to-launch-plexconnect-at-startup/page-5


global py

global un

global theKeychainItem

global thePassword

set py to “/path/to/PlexConnect_daemon.py” //change me

set theKeychainItem to “Name of Keychain Item” //change me

set un to getUsername(theKeychainItem)

set thePassword to getPassword(theKeychainItem)


do shell script "screen -A -m -d -S PlexConnect " & py user name un password thePassword with administrator privileges


on quit

do shell script "kill ps -u root | grep [P]lexConnect | grep [Pp]ython | awk '{print $2}'" user name un password thePassword with administrator privileges

continue quit

end quit


on getPassword(KeychainItem)

set theResult to do shell script “security 2>&1 find-generic-password -gs " & KeychainItem

set theResult to extractData(theResult, “password: “”, “””, 0)

return theResult

end getPassword


on getUsername(KeychainItem)

set theResult to do shell script “security 2>&1 find-generic-password -gs " & KeychainItem

set theResult to extractData(theResult, ““acct”=””, “””, 0)

return theResult

end getUsername


on extractData(theText, theFieldName, theEndDelimiter, spaces)

set theDataStart to the offset of theFieldName in theText

if theDataStart = 0 then

return “”

else

set theDataStart to theDataStart + (length of theFieldName) + spaces

set theData to text theDataStart through end of theText

set theDataEnd to ((offset of theEndDelimiter in theData) - 1)

set theData to text 1 through theDataEnd of theData

end if

end extractData

If PlexConnect.py is used directly then things will not get saved, cleaned up etc…
Please edit your posts as we do not want support issues from hacks that users are using when they are not aware of potential issues with them.

 

If PlexConnect.py is used directly then things will not get saved, cleaned up etc..
Please edit your posts as we do not want support issues from hacks that users are using when they are not aware of potential issues with them.

This is not entirely true, this is how it was long time ago (when CTRL-C was still in there), one of my older fixes fixed that problem already. When doing it correctly, that is, kill the right process, settings are saved correctly. However how do you know what process to kill? 

 

This is where the daemon version helps, it accepts an argument with a path for a file in which the process id of the mian thread can be saved. 

 

The second part for the daemon is disconnecting file inputs and outputs from the terminal so that it can keep on running when a terminal window is closed. For this launchctl does indeed already help, that  background process keeps the terminal connections open so that part of daemonising is not really needed on mac. 

 

Now about how will we get the settings saved? I have not investigated how the mac (launchctl) 'stops' the program, if it sends a 'terminate' command to the main thread then we're ok with just using PlexConnect if not the we need to be able to do a similar thing as what the PlexConnect_daemon.bash does when starting and stopping PlexConnect.

A script like PlexConnect_daemon.bash is what I use on the Synology NAS to correctly start and stop the program.

 

 

I had the same before but without the sleep. I now added the sleep as well and now it is also starting correctly at boot time. Thanks!

Sent from my iPhone using Tapatalk

I think I’ll wait until you guys come up with a consensus on the best way to do this. Just running the bash file via terminal after startup works for me since my computer is almost always on.

I think I'll wait until you guys come up with a consensus on the best way to do this. Just running the bash file via terminal after startup works for me since my computer is almost always on.


Ditto. I'm already confused by the conflicting suggestions in this thread.

Let me try to simplify what im telling you guys here. I use automator to make a app called plexconnect. I goto settings/users & groups/login items then add my plexconnect app to start when my machine starts, I also use my fastuser app to load pms @ boot without anyuser input (only needed if you have a password and want your apps to load and still keep your machine secure). Done. No messing with launchctl, no editing plexconnect.py. Future ready for any build of plexconnect. I personally don't like launchctl, I know how to use it, but making my own apps using automator seems to simplify things. I do use launchctl to load pms but you can also do this by checking load at login, but I like my plist because it keeps pms always running. Im not saying these other plists don't work but its just a lot more of a headache to try to constantly test plists by using lauchctl to load them in my experience. If you want to fight with launchctl and edit plexconnect.py have at it, if you want to make it easy on yourself use automator to make a app called plexconnect your choice :)

Let me try to simplify what im telling you guys here. I use automator to make a app called plexconnect. I goto settings/users & groups/login items then add my plexconnect app to start when my machine starts. Done. No messing with launchctl, no editing plexconnect.py. Future ready for any build of plexconnect. I personally don't like launchctl, I know how to use it, but making my own apps using automator seems to simplify things. I do use launchctl to load pms but you can also do this by checking load at login, but I like my plist because it keeps pms always running. Im not saying these other plists don't work but its just a lot more of a headache to try to constantly test plists by using lauchctl to load them in my experience.

I think we have got the point now ;)

However there are three reasons though why that is not the optimum solution for a lot of users (it does not stop them using it and it has been around since almost the inception of PlexConnect):

1> Security - the fact that by using an Applescript or Automator action you potentially expose you computers admin/root password giving an attacker full access to everything on your machine

2> Launchctl is good in that it will auto restart an application if it crashes

3> Your solution only works for OSX, daemon code is to allow all *nix flavours to benefit, that is OSX, Linux, all those NAS devices and more

1. If you only allow the plexconnect app to have access to this how is the password exposed? If thats the case any app/script thats shares the same password I would think in via keychain access is at risk. Unless there i something I am missing security wise here :P

2. True, but I never had plexconnect crash on me yet using this method.

3. I figured this was osx specific by the title, but I understand for other platforms for sure need to be cross compliant using the daemon.

If you see the original AppleScript thread it discusses the security risk around the method, it is small but does create an attack vector (I have made an assumption that you are using that or a similar method).

I am using the original applescript thread to create the app, never looked thru it to see the security issue in there. I figure if someone can hijack my mac they deserve to hack me :)

I spent some time trying to follow this thread.  I even followed the links to the github project.  I have observed with my mac mini, that I will always have one of the two primary accounts logged in.  Those primary accounts are, mine and my wife's.  In this case, I concocted a different solution where I centralized the media catalogs and channels in a folder in /var.  Then symlinked everything into the standard locations within the user account Library folder.  Additionally, I used launchctl to auto-launch on login.  This also resolves the crash and relaunch scenario.

For more specifics, check out my write up - Auto-launch Plex Media Server on Mac OS X.

I created this to autostart pms if you wanted to compare it to your solution. Nice write up :slight_smile:


http://forums.plexapp.com/index.php/topic/81127-plex-media-center-automatic-startup-applescript-login-option-osx/

Thank you.  I appreciate the time and effort that everyone has put in.  I think that solution would be great if I was more concerned about the security of my machine.  Worse case scenario, one of my kids gets in and uses the machine.  Not the worst case and they are pretty good about not using it unless asking ahead of time.  As I mentioned in my post I always have someone logged in and if not, the machine is 20 feet from my living room roku box.

Thanks again for the all effort.  It certainly provided some inspiration for the solution I came up with.

Ah, now this is something I'll find VERY useful. I've been keeping a terminal window open with PC running for a while, and on occasion have accidentally closed the window, which isn't great. Relaunching PC gives errors (DNS still running), so I have to remember to kill off any python processes and then re-run it. With this method I can fire and forget, until I reboot, which is rare, so this is ok.

Great work on this.

I’m a little confused by this thread. Is there an actual working launch daemon plist tutorial using the new PlexConnect_daemon.py?