Post Processing Script, Fails Calling WSL

Server Version#: 1.30.2.6563
Player Version#: N/A

OK, pulling my hair out with this one, and I can’t afford that … LOL. I have a Post Processing Script (bash script) that is working great on Linux, want to also use it in Windows => WSL! It had been working fine before, but seems to have stopped … and I can’t figure out why.

I call the WSL script from a batch (.bat) file => executes wsl.exe . Works fine from the command line, but not when Plex calls it - trying to figure out the difference. If I change the batch file to simply call (for example), wsl.exe whoami … very basic bash command. Again, works fine if I run it from a Windows console host or terminal (Powershell) … but again, just fails (hangs it seems?) when called from Plex.

Any thoughts at all? Is Plex using the console host (CMD), or terminal (Powershell)? Or is it going with the Windows setting for this? And is it running as a “special” user, which may be blocking WSL? Again, this is only failing when called from Plex, and I can’t even have it run “whoami” (to try a very basic bash command).

Thanks!

The only thing I remember is that it has to be in the /Scripts directory.

Postprocessing Script

If you have set up a script to perform postprocessing on recordings (prior to them being added to the library), specify the full path to the program here. Beginning with Plex Media Server v1.19.3, the script must be located inside the /Scripts subdirectory of the main server data directory.

Yes, agreed! And it’s there. It’s firing … but calling WSL (wsl.exe) fails => only when called from Plex. I have tried all sorts of manual executions, from CMD or Terminal, all good. Just Plex calling it fails - and again, it is firing, just won’t get through the .bat file.

Thanks!

Not using it myself, so guessing here…

Try and specify the full path to it, as C:\Windows\System32\wsl.exe

If running 64Bit Plex, it might default to SysWow64 instead

Agreed! I should have mentioned, my script “checks” this,

if exist %SystemRoot%\System32\wsl.exe set "FileNameWSL=%SystemRoot%\System32\wsl.exe" & goto RunWSLCommands
if exist %SystemRoot%\Sysnative\wsl.exe set "FileNameWSL=%SystemRoot%\Sysnative\wsl.exe" & goto RunWSLCommands

So it tries to check and run the correct version of WSL => seems to use System32, which is the 64-bit version, and that makes sense (I think :laughing:), as I have the 64-bit server running.

But you may be on to something - this issue only exists with Plex (my script runs fine manually), and only after some recent Windows 11 updates (I think this is part of the trigger)?

Any and all suggestions welcome :smile:. It’s just with Plex that this fails.

Thanks!

Since not using it, I can only suggest, that your script do something like:

if exist %SystemRoot%\System32\wsl.exe echo "Found in System32" >> c:\I_NEED_THIS_TO_WORK.txt
if exist %SystemRoot%\System32\wsl.exe set "FileNameWSL=%SystemRoot%\System32\wsl.exe" & goto RunWSLCommands

Agreed! I sort of am - let me try to explain, may help.

Here is a snippet of my code => adding some debug output to a log file,

set "logFile="D:\plexServer\Plex Media Server\Scripts\plexPostProcessing.log""
echo Starting ... > %logfile%
echo Time = %time% %date% >> %logfile%
echo whoami, %username% >> %logfile%
echo username, %username% >> %logfile%
echo And, postProcessing ... %1 >> %logfile%

REM Find Windows Subsystem for Linux (wsl.exe), safe for Plex as 32 or 64 bit (and calling a 32 or 64 bit shell!). Try the default folder first
REM   => More details at, https://stackoverflow.com/questions/70112271/why-is-the-wsl-command-not-found-by-cmd-on-starting-a-batch-file-with-a-double-c
REM Can use %SystemRoot%, or %windir%
if exist %SystemRoot%\System32\wsl.exe set "FileNameWSL=%SystemRoot%\System32\wsl.exe" & goto RunWSLCommands
if exist %SystemRoot%\Sysnative\wsl.exe set "FileNameWSL=%SystemRoot%\Sysnative\wsl.exe" & goto RunWSLCommands

REM Run my desired commands. %1 from Plex already includes double quotes ... so just pass %1, all good!
:RunWSLCommands
echo WSL: %FileNameWSL% >> %logfile%
echo Video File: %1 >> %logfile%
%FileNameWSL% /mnt/plexMedia/plexPostProcessing.sh %1
echo Done. >> %logfile%

If I manually run the script (CMD shell), plexPostProcessing.bat testFile, and then check the log file,

Starting ... 
Time = 16:02:36.47 Tue 01/31/2023 
whoami, micro 
username, micro 
And, postProcessing ... testFile 
WSL: C:\WINDOWS\System32\wsl.exe 
Video File: testFile 
Done. 

Now, on the WSL side, the code in the bash script,

echo "Starting wsl ..."
date
echo "$SHELL"
echo "Here ..." > /tmp/plexPostProcessing.log
date >> /tmp/plexPostProcessing.log
echo "$SHELL" >> /tmp/plexPostProcessing.log
echo $1 >> /tmp/plexPostProcessing.log

And, checking this, cat /tmp/plexPostProcessing.log,

Here ...
Tue Jan 31 16:02:36 CST 2023
/bin/bash
testFile

Yep, exactly as all expected. But then, try a recording from Plex, and the two log files … Windows,

Starting ... 
Time = 16:05:26.28 Tue 01/31/2023 
whoami, micro 
username, micro 
And, postProcessing ... "E:\plexArchive\TV Shows\.grab\02ff54d9796c1c184f11080618ba4dd68741d7cb-30ac5a226a4d374f329a8d841bd8aab7bd91d9c8\Family Feud (2010) - S20E136 - Family Feud.ts" 
WSL: C:\WINDOWS\System32\wsl.exe 
Video File: "E:\plexArchive\TV Shows\.grab\02ff54d9796c1c184f11080618ba4dd68741d7cb-30ac5a226a4d374f329a8d841bd8aab7bd91d9c8\Family Feud (2010) - S20E136 - Family Feud.ts" 
Done. 

Perfect! My script is being called. So then, on WSL,

Here ...
Tue Jan 31 16:02:36 CST 2023
/bin/bash
testFile

Nope :frowning_face:. Still the old contents. So only with Plex, I’m not getting over to WSL. Make sense?

Thanks!

As said, not using it myself, so again just guessing, but

I suspect above is always true
So maybe change script to a case statement, where you start to check for Sysnative

No worries! Appreciate the guesses, NP at all. Makes me think as well :grin:.

I tried adding to the batch file,

where wsl >> %logfile%

In both cases, I get,

C:\Windows\System32\wsl.exe
C:\Users\micro\AppData\Local\Microsoft\WindowsApps\wsl.exe

Interesting to see 2 of them, but in any case it seems C:\Windows\System32\wsl.exe is correct, agreed? I did also try forcing Sysnative, and that just gave me an error message. So seems the call is right. For some reason, only when called from Plex, it never gets to WSL. That’s so odd. What else could Plex be setting to cause it. Hmmm.

Guess away … :laughing:. Digging here in parallel.

Thanks again.

Maybe: How to execute WSL commands from a windows batch file? · Discussion #6128 · microsoft/WSL · GitHub

That makes sense, the whole 32 / 64 bit thing. Tried it - no difference (and tried both Sysnative as well as System32).

One thing that is interesting - when I run manually, and it works … the exit (return) code is 3. But in the failed case (called from Plex), the exit code is 1. Hmmm. Trying to find what those exit codes mean :laughing:.

Thanks!

OK, found it! Sharing for others, in case they have the same issue :smile:.

I stumbled on to this, as part of a server update, but the issue only occurs if PMS is controlled by PmsService. Posted the details here (again, just to share).

Thanks for the thoughts and pointers, much appreciated!