Plex Media Server as Startup Task in 2012R2

Hello,

I see many people here want Plex to startup with the PC regardless of whether a user is logged in. I like the idea of the PMS as a service wrapper, but always prefer to do things natively in the OS if possible. I don't really want to move my library to a location and set permissions etc to make it work with any user logged in.

Also, I use Server 2012R2 which is allocated 2 Administration RDP connections, so leaving a user logged in 24/7 is kind of a waste, as well as a security issue if you don't set it to lock right away.

As it turns out, it is easy to do once you figure out what the issue is exactly. The issue is a bug in newer versions of Windows as seen at http://support.microsoft.com/kb/2968540.

Essentially what happens is, if you launch a scheduled task it ends up starting before the user path variables are populated so the task ends up running under the desired user's credentials but using the Default profile folder. The Default profile is hidden and can be accessed via C:\Users\Default. This is why the workaround listed in the MS link is to launch an interactive task before the actual task with a 1-2 minute delay between them. This gives the system time to load the profile.

I have found a better way to get around this, which is to use a batch file to set the correct profile variables, then launch PMS. To do this just create a batch file, and place it in your Plex Media Server install folder (Probably C:\Program Files (x86)\Plex\Plex Media Server\)

The batch file should contain the following code;

@rem Sets user profile to desired directory prior to lauching PMS EXE as a scheduled task.
set "USERPROFILE=%USERPROFILE%\..\%USERNAME%"
set "APPDATA=%USERPROFILE%\AppData\Roaming"
set "LOCALAPPDATA=%USERPROFILE%\AppData\Local"
"%~dp0\Plex Media Server.exe"

Now in your scheduled task, just call the batch file that you just created and your library will load properly from whatever user is actually running the task.

To create a task open the Task Scheduler and Create a New Task (not Basic).

  1. On the general tab
    1. Name your task
    2. Give it a description if you like
    3. Select "Change User or Group..." and pick the user that has the Media Library set up.
    4. Select "Run whether user is logged on or not"
    5. Change "Configure for:" to your version of Windows
  2. On the Triggers tab
    1. Select "New"
      1. Set "begin the task" to "At startup"
      2. Select OK
  3. On the Actions Tab
    1. Select "New"
      1. Select "Browse" and locate your batch file. For example mine is "C:\Program Files (x86)\Plex\Plex Media Server\PMS Launcher.bat"
      2. Select OK
  4. On the Conditions tab
    1. Uncheck "Start the task only if the computer is on AC power" if you are using a laptop.
  5. On the Settings tab
    1. Uncheck "Stop the task if it runs longer than"
    2. Select OK
    3. Enter the credentials for the user you picked on the first tab.

Reboot your computer and then login as any other user. Open the task manager and see if "Plex Media Server" is running.

If it is not running go back into the Task Scheduler and see why the task did not run under the "last run result" column. The PMS account needs to be local admin as far as I know. If not you can try using the "Run with highest privledges" check box on the general tab of the task but this might break the batch file.


If it is running, try to access your libraries with the local Plex URL like

http://YOUR.SERVER.IP.ADDRESS:32400/web/index.html

If your libraries are listed then you are good to go. If you get blank libraries then then something went wrong.

 

MS has told some people that they will not be fixing the bug, as a business case has not been made with an impact high enough to warrant the change.

FYI this is tested and used on Server 2012R2, but should also work on Windows 8/8.1.

2 Likes

I still believe this is a much easier option: PMS as a Service and you can control which account the service runs under. In my case I install Plex Media Server with an admin account but actually run it (as a service) in a non-admin account.

Like I said I prefer to not use 3rd party software if something can be done natively in the OS (especially if it is pretty easily done). Anyway I think the more options people have the better no?

I prefer my method as it is easy to do, and does not require any maintenance later. Currently using any unsupported 3rd party app means that Plex could unintentionally/accidently change something and break the app at any time. The sever I am running supports lots of users (for Plex and other things) so the less things that can break the better. I also prefer to let PMS update automatically and from my understanding you have to disable the wrapper, update, then re-enable.

Anyway others may prefer to just set the computer to auto login. Which is fine too. I'm just offering an additional way to do it.

Tasks can be run under any user account that you prefer. Also, if you don't want to give the user admin access you can easily use "run as" in the batch file, or you can try setting the toggle to run the service at the highest privilege level (which will probably work but I have never tested it).

1 Like

Newby with newby question:

The batch file should contain the following code;
@rem Sets user profile to desired directory prior to lauching PMS EXE as a scheduled task.
set “USERPROFILE=%USERPROFILE%…%USERNAME%”
set “APPDATA=%USERPROFILE%\AppData\Roaming”
set “LOCALAPPDATA=%USERPROFILE%\AppData\Local”
“%~dp0\Plex Media Server.exe”

Is this literal, or do I substitute my user name for where you have the word “USERNAME” ?

%USERNAME% is an environment variable - automatically filled in

But for a newbie I would recommend going for the packaged solution : PMS As A Service

OP (bjd223 ) you nailed it. This works perfectly on Windows 10 and simple way of controlling it without any additional software. Simply copy n paste into bat file exactly what is printed and follow the instructions moonedust it works.

I can also confirm running the application at highest level (aka admin) works without causing any issue in windows 10 although I cant see why it would be needed.

And like OP I agree with why add more software when natively it’s not needed. Actually if Plex added the bat file and then option to write task in the installer then they wouldn’t need any additional software at all.

Cheers
Red

@moonedust said:
Newby with newby question:

The batch file should contain the following code;
@rem Sets user profile to desired directory prior to lauching PMS EXE as a scheduled task.
set “USERPROFILE=%USERPROFILE%..%USERNAME%”
set “APPDATA=%USERPROFILE%\AppData\Roaming”
set “LOCALAPPDATA=%USERPROFILE%\AppData\Local”
“%~dp0\Plex Media Server.exe”

Is this literal, or do I substitute my user name for where you have the word “USERNAME” ?

It is literal. Just copy the exact code unchanged into a .bat file and save it.

Worked beautifully on Server 2012 R2. Thanks for the post

This works like a charm on windows 10.
Easy to set up, no additional software or maintenance required. Simply perfect.

Thank You!

This is great, solved so many of my problems with my kids logging my main account out or restarting the machine and then our server goes down until I realize it. Works perfect!

MS released a hotfix which fixes the underlying issue here https://support.microsoft.com/en-us/kb/3133689

This means you can call the PMS exe directly in task scheduler and it will use the correct profile without using the batch file above.

This patch applies to Windows Server 2012 R2 Datacenter, Windows Server 2012 R2 Standard, Windows Server 2012 R2 Essentials, Windows Server 2012 R2 Foundation, Windows 8.1 Enterprise, Windows 8.1 Pro, Windows 8.1, Windows RT 8.1

So far so good. :slight_smile:

Thanks for taking the time to post this fixe. I remember using scheduled tasks back in the day to run “Disk Cleanup” and “Defrag”. Thankful for SSDs.

Any caveats or cautions for Server 2016?

Also, @bjd223 - before I actually attempt it, what did Microsoft fix in their hotfix? The environment variables? So, if I’m understanding, you don’t need the batch file at all, and step 3.1.1 might be:

  • Select “Browse” and locate the PMS executable. By default this will be “C:\Program Files (x86)\Plex\Plex Media Server\Plex Media Server.exe”
  • ???
  • Profit!!1

If so, would it be too much to ask you to update your original post with the new procedure and a note that it was fixed by Microsoft? If not, can you clarify what’s different?

(also, if anyone knows what’s up, my executable is in the root of “Program Files x86”, which… surprised me - is that particularly abnormal?)

@brettpeirce said:
Any caveats or cautions for Server 2016?

Also, @bjd223 - before I actually attempt it, what did Microsoft fix in their hotfix? The environment variables? So, if I’m understanding, you don’t need the batch file at all, and step 3.1.1 might be:

  • Select “Browse” and locate the PMS executable. By default this will be “C:\Program Files (x86)\Plex\Plex Media Server\Plex Media Server.exe”
  • ???
  • Profit!!1

If so, would it be too much to ask you to update your original post with the new procedure and a note that it was fixed by Microsoft? If not, can you clarify what’s different?

(also, if anyone knows what’s up, my executable is in the root of “Program Files x86”, which… surprised me - is that particularly abnormal?)

Yes your directions look correct. The task should run under the correct user profile for the username running the task. If Plex puts files in the “Public” user profile then your system is not patched or the patch is not working correctly.

Using the batch file with the patch will not hurt anything, and it will function exactly the same. Since it is an optional patch I am not sure if it is included in 2016 or not.

1 Like

Im with @bjd223 on this one! Worked perfectly on my WS2012R2! Thank you for sharing

Thanks! Works perfectly and took less than a minutes to set up!

1 Like

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