Beginner help with custom Python script

Sonarr version
Mono version (if Sonarr is not running on Windows):
OS: Windows 10 (21H2):
Debug logs:

2022-11-07 15:21:42.8|Debug|CustomScript|Executing external script: M:\scripts\sonarr\execute.bat
2022-11-07 15:21:42.8|Debug|cmd.exe|Starting cmd.exe /c M:\scripts\sonarr\execute.bat 
2022-11-07 15:21:42.9|Debug|cmd.exe|C:\Windows\system32>"C:\Users\(removed)\AppData\Local\Programs\Python\Python310\python.exe" "M:\scripts\sonarr\" 
2022-11-07 15:21:42.9|Error|cmd.exe|Access is denied.
2022-11-07 15:21:43.0|Debug|CustomScript|Executed external script: M:\scripts\sonarr\execute.bat - Status: 1
2022-11-07 15:21:43.0|Debug|CustomScript|Script Output: 
07.11.2022 14:21:42 - Standard - C:\Windows\system32>"C:\Users\(removed)\AppData\Local\Programs\Python\Python310\python.exe" "M:\scripts\sonarr\" 
07.11.2022 14:21:42 - Error - Access is denied.
2022-11-07 15:21:43.0|Warn|SonarrErrorPipeline|Invalid request Validation failed: 
 -- : Script exited with code: 1

Description of issue:

I am trying to make a Python test custom script to see if I can get this working on windows.

If I set Sonarr to run my Python script M:\scripts\sonarr\ I get the error “The system cannot find the file specified

However, if I change the path to a BAT file I get a different error: “Script exited with code: 1”, in other words, something went wrong.

I have checked:

:heavy_check_mark: Python is added to PATH

My directories are:

📂 M:
┣ 📂 scripts
┃ ┣ 📂 sonarr
┃ ┃ ┣ 📃 execute.bat
┃ ┃ ┣ 📃

:page_with_curl: execute.bat:

python "M:\scripts\sonarr\"


from os import environ

    test = environ.get('sonarr_eventtype')
except Exception as e:
    raise e


Are you running sonarr as a service?

Yes. I am pretty sure. It is listed in services and running as “Local Service”.

OK. So per the caveat listed on the download page, and the errors logged, Sonarr likely doesn’t have sufficient access to do what you’re asking. Fix accordingly and try again.

A Windows Service runs even when the user is not logged in, but special care must be taken since Windows Services cannot access network drives (X:\ mapped drives or \server\share UNC paths) without special configuration steps.
Additionally the Windows Service runs under the ‘Local Service’ account, by default this account does not have permissions to access your user’s home directory unless permissions have been assigned manually. This is particularly relevant when using download clients that are configured to download to your home directory.
It’s therefore advisable to install Sonarr as a System Tray Application if the user can remain logged in. The option to do so is provided during the installer.

Thanks for the suggestion. I mean, I can try to reinstall Sonarr as a non-service, but it doesn’t really make sense. Sonar is already downloading to the same drive. This is not the “home directory” as explained, either.

Has anyone ever managed to run a custom script for Sonarr on Windows? It could help to try and replicate working examples. But all the examples I could find was for Sonarr 2.

But it does make sense?

The errors you posted show access denied errors.
Your Python shows as being in your user appdata folder, which is a part of your “home” directory structure, which as mentioned the local service account does not have access to by default.

So, sonarr likely doesn’t have sufficient access to do what you’re asking. Fix accordingly and try again.

Change the account the service runs as
Add Folder permissions for the current service account
Change from service to app

You were absolutely correct. :blush::+1: I installed Sonarr as a non-service, and the script works.

I was running Sonarr as a service because I only access the computer with Remote Desktop. However, with a non-service install, it seems like Sonarr works anyway, as long as I log in with RDP first, then disconnect the RDP session. I hope it stays that way.

Thanks for guiding me through this!

1 Like

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