# Post Processing Script, Fails Calling WSL

**URL:** <https://forums.plex.tv/t/post-processing-script-fails-calling-wsl/828857>\
**Category:** Plex Media Server\
**Tags:** livetv-dvr, server-windows\
**Created:** [January 30, 2023, 1:10pm UTC](https://forums.plex.tv/t/post-processing-script-fails-calling-wsl/828857 "2023-01-30T13:10:51Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![rmorris](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/rmorris/32/13669_2.png) [@rmorris](https://forums.plex.tv/u/rmorris)\
**Post date:** [January 30, 2023, 1:10pm UTC](https://forums.plex.tv/t/post-processing-script-fails-calling-wsl/828857/1 "2023-01-30T13:10:51Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![pl\_5309](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@pl\_5309](https://forums.plex.tv/u/pl_5309)\
**Post date:** [January 30, 2023, 1:17pm UTC](https://forums.plex.tv/t/post-processing-script-fails-calling-wsl/828857/2 "2023-01-30T13:17:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rmorris](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/rmorris/32/13669_2.png) [@rmorris](https://forums.plex.tv/u/rmorris)\
**Post date:** [January 30, 2023, 1:20pm UTC](https://forums.plex.tv/t/post-processing-script-fails-calling-wsl/828857/3 "2023-01-30T13:20:22Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![dane22](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/dane22/32/4389_2.png) [@dane22](https://forums.plex.tv/u/dane22)\
**Post date:** [January 31, 2023, 3:32pm UTC](https://forums.plex.tv/t/post-processing-script-fails-calling-wsl/828857/4 "2023-01-31T15:32:50Z")

</div>

> [@rmorris](#):
>
> wsl.exe

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

---

<div class="post-metadata">

**Author:** ![rmorris](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/rmorris/32/13669_2.png) [@rmorris](https://forums.plex.tv/u/rmorris)\
**Post date:** [January 31, 2023, 9:26pm UTC](https://forums.plex.tv/t/post-processing-script-fails-calling-wsl/828857/5 "2023-01-31T21:26:59Z")

</div>

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

```auto
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 😆), 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 😄. It’s just with Plex that this fails.

Thanks!

---

<div class="post-metadata">

**Author:** ![dane22](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/dane22/32/4389_2.png) [@dane22](https://forums.plex.tv/u/dane22)\
**Post date:** [January 31, 2023, 9:34pm UTC](https://forums.plex.tv/t/post-processing-script-fails-calling-wsl/828857/6 "2023-01-31T21:34:02Z")

</div>

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

```auto
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

```

---

<div class="post-metadata">

**Author:** ![rmorris](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/rmorris/32/13669_2.png) [@rmorris](https://forums.plex.tv/u/rmorris)\
**Post date:** [January 31, 2023, 10:07pm UTC](https://forums.plex.tv/t/post-processing-script-fails-calling-wsl/828857/7 "2023-01-31T22:07:53Z")

</div>

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,

```auto
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,

```auto
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,

```auto
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`,

```auto
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,

```auto
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,

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

```

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

Thanks!

---

<div class="post-metadata">

**Author:** ![dane22](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/dane22/32/4389_2.png) [@dane22](https://forums.plex.tv/u/dane22)\
**Post date:** [January 31, 2023, 11:08pm UTC](https://forums.plex.tv/t/post-processing-script-fails-calling-wsl/828857/8 "2023-01-31T23:08:04Z")

</div>

> [@rmorris](#):
>
> `if exist %SystemRoot%\System32\wsl.exe`

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`

---

<div class="post-metadata">

**Author:** ![rmorris](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/rmorris/32/13669_2.png) [@rmorris](https://forums.plex.tv/u/rmorris)\
**Post date:** [February 1, 2023, 12:04am UTC](https://forums.plex.tv/t/post-processing-script-fails-calling-wsl/828857/9 "2023-02-01T00:04:18Z")

</div>

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

I tried adding to the batch file,

```auto
where wsl >> %logfile%

```

In both cases, I get,

```auto
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 … 😆. Digging here in parallel.

Thanks again.

---

<div class="post-metadata">

**Author:** ![dane22](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/dane22/32/4389_2.png) [@dane22](https://forums.plex.tv/u/dane22)\
**Post date:** [February 1, 2023, 12:11am UTC](https://forums.plex.tv/t/post-processing-script-fails-calling-wsl/828857/10 "2023-02-01T00:11:31Z")

</div>

Maybe: [How to execute WSL commands from a windows batch file? · Discussion #6128 · microsoft/WSL · GitHub](https://github.com/microsoft/WSL/discussions/6128)

---

<div class="post-metadata">

**Author:** ![rmorris](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/rmorris/32/13669_2.png) [@rmorris](https://forums.plex.tv/u/rmorris)\
**Post date:** [February 1, 2023, 12:35am UTC](https://forums.plex.tv/t/post-processing-script-fails-calling-wsl/828857/11 "2023-02-01T00:35:17Z")

</div>

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

Thanks!

---

<div class="post-metadata">

**Author:** ![rmorris](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/rmorris/32/13669_2.png) [@rmorris](https://forums.plex.tv/u/rmorris)\
**Post date:** [February 2, 2023, 2:20pm UTC](https://forums.plex.tv/t/post-processing-script-fails-calling-wsl/828857/12 "2023-02-02T14:20:24Z")

</div>

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

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](https://github.com/cjmurph/PmsService/issues/73) (again, just to share).

Thanks for the thoughts and pointers, much appreciated!

---

<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:** [May 3, 2023, 2:21pm UTC](https://forums.plex.tv/t/post-processing-script-fails-calling-wsl/828857/13 "2023-05-03T14:21:19Z")

</div>

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