The Core Technologies Blog

Professional Software for Windows Services / 24×7 Operation


Q&A: How do I Avoid Errors when Stopping my Plex Windows Service?

Q & A - Shutdown Plex Windows Service
  I’ve download the trial version of AlwaysUp and installed it on a Server 2016 standard machine. I’ve created a Windows service for Plex Media Server and all is working well except for shutting the service down (or restarting).

Every time I restart or shut down the service I’m getting this error:

The application has failed to stop gracefully and may be forcibly terminated. The following processes may be terminated: Plex Media Server.exe, PlexScriptHost.exe, conhost.exe, PlexScriptHost.exe, conhost.exe, Plex Tuner Service.exe, conhost.exe

Can you please let me know what’s wrong?

— Panja

Hi Panja. We can fix this 🙂

But first, a brief explanation of what is going on…

Plex Media Server (PMS) launches several processes

When you start PMS (either normally on your desktop or as a service from AlwaysUp), the main executable (Plex Media Server.exe) spawns a family of processes. Those processes work together, invisibly in the background, to share your files with hungry devices.

You can see the “tree” of up to 8 processes with Microsoft’s Process Explorer:

Plex Media Server Processes

Exiting PMS from the tray icon closes all the processes

When Plex is running normally on your desktop, you close it by selecting “Exit” from the tray icon menu. The main process receives the signal and quietly closes the other 7 processes. You never witness the behind-the-scenes coordination.

AlwaysUp tries several methods to exit PMS and close all its processes

When you are running Plex Media Server as a background service, there is no tray icon. You must rely on AlwaysUp to shut down PMS and its family of processes.

AlwaysUp tries up to four methods to close the media server properly. The error message you see is reported because the first two methods are unsuccessful and AlwaysUp must move on to the last two, more “aggressive” tactics.

However, looking at the logs, the third stop-method works for Plex. The server isn’t actually being “forcibly terminated”. So though annoying, you can safely ignore the error — it is a false alarm.

But who likes to see a useless error message, right? Here are a couple of ways to eliminate it entirely. Choose one or the other; no need to do both:

Solution #1: Remove the “nointeractive” flag from the arguments passed to Plex

In our step-by-step tutorial, we recommend running Plex Media Server.exe with the -nointeractive flag. This tells Plex that it should not create a tray icon because we cannot access it when PMS is running in the background (in Session 0).

However, when the flag is specified, Plex doesn’t create a main window either. And without a main window, AlwaysUp cannot send Plex the “default” signal to close.

Without the flag, AlwaysUp is able to close Plex smoothly — sans error messages.

To remove the nointeractive flag, edit your PMS entry in AlwaysUp and clear the Arguments field:

Clear PMS service arguments

There is only one (minor) downside to this solution. Without the flag, Plex will print a warning to its log file every few seconds. This won’t be a problem — unless you are a techie digging into the Plex internals — but it deserves to be mentioned.

Plex Media Server Log File

Solution #2: Stop Plex with taskkill

The second alternative is to plug in a separate program to close PMS. Several folks on the Plex forums recommend using the Windows taskkill command.

To configure AlwaysUp to shut down Plex with taskkill:

  1. Create a batch file with this line:

    taskkill /t /f /im “Plex Media Server.exe”

    As you can see from the taskkill documentation, this command will terminate PMS and all its child processes.

  2. Start AlwaysUp and edit your Plex Media Server entry

  3. Switch to the Extras tab.

  4. Enter the full path to the batch file you created in step 1 in the Use this special command to stop the application field. And you might as well give taskkill up to 30 seconds to do its work by completing the field below that:

    Stop Plex Media Server with taskkill
  5. Save your settings

With this solution in place, the next time you stop PMS from AlwaysUp:

  • AlwaysUp will run the batch file

  • The batch file will run taskkill

  • taskkill will close all the PMS processes

  • AlwaysUp will see that PMS has closed and will set the state of the Plex Media Server entry to “Stopped”.

No error messages!

Posted in AlwaysUp | Tagged , , , | Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *