Script won't run, it's gotta be something simple

Get help with all aspects of SABnzbd
Forum rules
Help us help you:
  • Are you using the latest stable version of SABnzbd? Downloads page.
  • Tell us what system you run SABnzbd on.
  • Adhere to the forum rules.
  • Do you experience problems during downloading?
    Check your connection in Status and Interface settings window.
    Use Test Server in Config > Servers.
    We will probably ask you to do a test using only basic settings.
  • Do you experience problems during repair or unpacking?
    Enable +Debug logging in the Status and Interface settings window and share the relevant parts of the log here using [ code ] sections.
Post Reply
spamphox
Newbie
Newbie
Posts: 7
Joined: March 1st, 2025, 2:42 pm

Script won't run, it's gotta be something simple

Post by spamphox »

Hi frens,

I'm on version 4.4.1 and am trying to get a very simple bash post-processing script that will run via category to copy a file to another directory upon completion. I've read the wiki, countless forum posts, and every relevant google search. Upon download completion, Sab tries to run the following script

Code: Select all

#!/bin/bash
cp -r /data/00_Watch_Folders/00_landing_pad/$1/ /data/00_Watch_Folders/0_watch_tv/
Which gives this error:
Exit(-1): Cannot run script /config/scripts/TV_script(.)sh
Sab is running in a docker as user "nobody." The script is owned by "nobody" and has 777 permissions. All directories have full rwx (777). I've verified the script is executable and ran it via console, which pulls in the empty "00_landing_pad" directory.

I ran debug logs which show the following:
[2025-03-01 12:18:54,437::DEBUG::[newsunpack:261] Failed script /config/scripts/TV_script(.)sh, Traceback:
Traceback (most recent call last):
File "/usr/lib/sabnzbd/sabnzbd/newsunpack(.)py", line 246, in external_processing
p = build_and_run_command(command, env=create_env(nzo, extra_env_fields))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/sabnzbd/sabnzbd/misc(.)py", line 1262, in build_and_run_command
return subprocess.Popen(command, **popen_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/subprocess(.)py", line 1026, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/lib/python3.12/subprocess(.)py", line 1955, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/config/scripts/TV_script(.)sh'
I've tried the source directory as "$1" and "/$1" with the same error. I also briefly tried a python script but gave up quickly because it came back with "$1" as invalid syntax and thought the first " _" in my watch folder was an invalid decimal literal.

I'm a filthy commoner with no programming and minimal scripting experience, but I can usually stumble my way through these types of problems. This one has had be perplexed for 3 days now and I'm about to the point where I've lost more time than I ever would have saved with this script.

Appreciate anyone's insight on what I'm messing up!
User avatar
sander
Release Testers
Release Testers
Posts: 9255
Joined: January 22nd, 2008, 2:22 pm

Re: Script won't run, it's gotta be something simple

Post by sander »

FileNotFoundError: [Errno 2] No such file or directory: '/config/scripts/TV_script(.)sh'

... what are those ( and ) doing there?
spamphox
Newbie
Newbie
Posts: 7
Joined: March 1st, 2025, 2:42 pm

Re: Script won't run, it's gotta be something simple

Post by spamphox »

Sorry for any confusion. I entered those manually because the forum was detecting the ".sh" as a link and I'm not allowed to post links as a new forum member. I work in cyber security and that's how we share links without making them accidentally 'clickable.'
User avatar
sander
Release Testers
Release Testers
Posts: 9255
Joined: January 22nd, 2008, 2:22 pm

Re: Script won't run, it's gotta be something simple

Post by sander »

OK.

Check /config/scripts/TV_script.sh does exist (from the point of view of SAB) and it's x bit is on.
spamphox
Newbie
Newbie
Posts: 7
Joined: March 1st, 2025, 2:42 pm

Re: Script won't run, it's gotta be something simple

Post by spamphox »

I verified x bit is ON.

I'm able to select the script from the "script" drop-down list in the "categories" menu of Sab. Is there another way to check that it exists to Sab?
User avatar
sander
Release Testers
Release Testers
Posts: 9255
Joined: January 22nd, 2008, 2:22 pm

Re: Script won't run, ... and post-processing scripts are always difficult: trial and error

Post by sander »

spamphox wrote: March 1st, 2025, 6:05 pm I verified x bit is ON.

I'm able to select the script from the "script" drop-down list in the "categories" menu of Sab. Is there another way to check that it exists to Sab?
Good!

I did some experiments for you:

Code: Select all

#!/bin/bash
echo "Directory" $1
echo "job name" $3
echo "All OK"
exit 0
works as it should. So: can you try that a post-processing script? First things first.

Then a faulty script

Code: Select all

#!/does/not/exist

echo Hallo
does not work (she-bang wrong), and ... results in

ScriptExit(-1): Cannot run script /home/sander/sab-scripts/faulty-script.sh

plus a traceback

Code: Select all

2025-03-02 08:48:23,954::DEBUG::[newsunpack:252] Failed script /home/sander/sab-scripts/faulty-script.sh, Traceback: 
Traceback (most recent call last):
  File "/usr/share/sabnzbdplus/sabnzbd/newsunpack.py", line 237, in external_processing
    p = build_and_run_command(command, env=create_env(nzo, extra_env_fields))
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/share/sabnzbdplus/sabnzbd/misc.py", line 1307, in build_and_run_command
    return subprocess.Popen(command, **popen_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/subprocess.py", line 1026, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.12/subprocess.py", line 1955, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/home/sander/sab-scripts/faulty-script.sh'

TIL: if inside the script there is a fault, SABnzbd will "Cannot run script" (fair enough) but also "FileNotFoundError: [Errno 2] No such file or directory: " which I find confusing

So I think the error is inside your script itself.
User avatar
sander
Release Testers
Release Testers
Posts: 9255
Joined: January 22nd, 2008, 2:22 pm

Re: Script won't run, it's gotta be something simple

Post by sander »

Code: Select all

cp -r /data/00_Watch_Folders/00_landing_pad/$1/ /data/00_Watch_Folders/0_watch_tv/
... that path is very probably wrong: The $1 is already the full path, and so /data/00_Watch_Folders/00_landing_pad/$1/ does not exist.
spamphox
Newbie
Newbie
Posts: 7
Joined: March 1st, 2025, 2:42 pm

Re: Script won't run, it's gotta be something simple

Post by spamphox »

Wow, thanks for all your effort.

$1 as a source directory gives the same error.

I actually had "$1" at first but changed it during troubleshooting. When I ran the script manually via the container's console, $1 caused it to try copying the container's entire directory over, so I changed the source to "...landing_pad/$1" which would only copy the landing pad directory when run manually.

I rewrote the script with the following and let it run with a test download:

Code: Select all

#!/bin/bash
echo "Directory" $1
echo "job name" $3
echo "All OK"
exit 0
Unfortunately, it gives the same exact error. I ran it in the container's console and it returned blank values for $1 and $3, which I would expect since it wasn't running after an actual job.
[nobody@dd38d12f8d0c /]$ cd config/scripts
[nobody@dd38d12f8d0c scripts]$ bash TV_script.sh
Directory
job name
All OK
[nobody@dd38d12f8d0c scripts]$
I had the same thought that a script error was causing Sab to declare there's "no such file." My gut feeling was a permission issue. I'm running the container as UID 99, GUID 100, UMASK 000. I've verified X-bit, with the script showing rwx across the board.

I've also tried running the container as a different user, chown'd all files to that user, chmod +x'd it, and verified rwxrwxrwx on all directories just to eliminate permissions as a factor somewhere I wasn't looking. It returned the same error.

I also ran the container in privileged mode, with the same result. I'm running the Binhex-Sabnzbd docker on Unraid, which offers "shell" and "bash" as options for "console shell command." I tried switching between the two with no change in result.

I've triple checked about 10 times now that all directories are owned by the user, permissions are set as rwxrwxrwx across the board, and all paths are correctly mapped and accessible. Sab can see the script, but the error message, "Exit(-1): cannot run script" is making me wonder if it's failing to even open.

I found this post from a decade ago where someone else had the same issue. Their solution was to give up on bash and use python.
https://forums.unraid.net/topic/37208-s ... ipt-error/
Last edited by spamphox on March 2nd, 2025, 11:02 am, edited 2 times in total.
spamphox
Newbie
Newbie
Posts: 7
Joined: March 1st, 2025, 2:42 pm

Re: Script won't run, it's gotta be something simple

Post by spamphox »

Your comment about the shebang had me wondering if maybe it couldn't execute bash for some reason? "which bash" returned the following:

Code: Select all

[nobody@dd38d12f8d0c /]$ which bash
/usr/sbin/bash
[nobody@dd38d12f8d0c /]$  
I edited the script with

Code: Select all

#!/usr/sbin/bash
and

Code: Select all

#!/usr/bin/bash
which also gave the exact same error & log entry. I checked permissions on all 3, with all showing
-rwxr-xr-x
They also show "root root" for user and group...it should still execute for any user based on the permissions it shows, but is it possible this would cause a problem somehow?
User avatar
sander
Release Testers
Release Testers
Posts: 9255
Joined: January 22nd, 2008, 2:22 pm

Re: Script won't run, it's gotta be something simple

Post by sander »

From where are you editing your script file? Windows perhaps ... ? If so: don't.

Because after using unix2dos to put windows-line-endings in the same shell script, SABnzbd says:

Code: Select all

ScriptExit(-1): Cannot run script /home/sander/sab-scripts/ugly-windows-line-endings.sh
and

Code: Select all

2025-03-02 16:59:08,134::DEBUG::[newsunpack:252] Failed script /home/sander/sab-scripts/ugly-windows-line-endings.sh, Traceback: 
Traceback (most recent call last):
  File "/usr/share/sabnzbdplus/sabnzbd/newsunpack.py", line 237, in external_processing
    p = build_and_run_command(command, env=create_env(nzo, extra_env_fields))
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/share/sabnzbdplus/sabnzbd/misc.py", line 1307, in build_and_run_command
    return subprocess.Popen(command, **popen_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/subprocess.py", line 1026, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.12/subprocess.py", line 1955, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/home/sander/sab-scripts/ugly-windows-line-endings.sh'
Note the ugly 0d 0a in the hexdump below:

Code: Select all

$ hd ugly-windows-line-endings.sh 
00000000  23 21 2f 62 69 6e 2f 62  61 73 68 0d 0a 0d 0a 65  |#!/bin/bash....e|
00000010  63 68 6f 20 22 44 69 72  65 63 74 6f 72 79 22 20  |cho "Directory" |
00000020  24 31 0d 0a 65 63 68 6f  20 22 6a 6f 62 20 6e 61  |$1..echo "job na|
00000030  6d 65 22 20 24 33 0d 0a  65 63 68 6f 20 22 41 6c  |me" $3..echo "Al|
00000040  6c 20 4f 4b 22 0d 0a 0d  0a 65 78 69 74 20 30 0d  |l OK"....exit 0.|
00000050  0a 0d 0a                                          |...|
00000053

Code: Select all

sander@zwarte:~/sab-scripts$ ./ugly-windows-line-endings.sh aaa bbb ccc
bash: ./ugly-windows-line-endings.sh: cannot execute: required file not found


sander@zwarte:~/sab-scripts$ sh ./ugly-windows-line-endings.sh aaa bbb ccc
: not foundows-line-endings.sh: 2: 
Directory aaa
job name ccc
All OK
: not foundows-line-endings.sh: 6: 
spamphox
Newbie
Newbie
Posts: 7
Joined: March 1st, 2025, 2:42 pm

Re: Script won't run, it's gotta be something simple

Post by spamphox »

Holy smokes! Yes, I was...I typically use notepad++. I deleted the script and recreated it from within the console, and it works as expected now! I NEVER would have thought of that being the crux of the issue on my own.

Code: Select all

#!/bin/bash
cp "$1"/* /data/00_Watch_Folders/0_watch_tv/
echo "Completed & Copied"
exit 0
The file successfully copies and Sab returns "Completed & Copied"

This had me going bonkers, Thanks so much for your help and insight!
User avatar
sander
Release Testers
Release Testers
Posts: 9255
Joined: January 22nd, 2008, 2:22 pm

Re: Script won't run, it's gotta be something simple

Post by sander »

Great.

Question: was this ... "something simple"?

I find it annoying that SABnzbd can't say something more clever than "No such file or directory"; the file is there, so the message is confusing. It just contains errors.

The root cause is probably because even when running directly, Linux itself says that:

Code: Select all

$ ./ugly-windows-line-endings.sh 
bash: ./ugly-windows-line-endings.sh: cannot execute: required file not found
This might explain it more (from https://unix.stackexchange.com/a/721845 ):

"Your demo.sh script is a DOS text file. Such files have CRLF line endings, and that extra CR (carriage-return) character at the end of the line is causing you issues.

The specific issue it's causing is that the interpreter pathname on the #!-line now refers to something called /bin/bash\r (with the \r symbolising a carriage-return, which is a space-like character, so it's usually not visible). This file is not found, so this is what causes your error message."

Theoretically, SABnzbd on Linux could pre-check for Windows line-ending CRLF, but I think that's quite ugly.

Ah, the check itself is easy from Linux CLI:

Code: Select all

sander@zwarte:~/sab-scripts$ file ugly-windows-line-endings.sh 
ugly-windows-line-endings.sh: Bourne-Again shell script, ASCII text executable, with CRLF line terminators

... note those last words.
spamphox
Newbie
Newbie
Posts: 7
Joined: March 1st, 2025, 2:42 pm

Re: Script won't run, it's gotta be something simple

Post by spamphox »

Wow, that's very deep for someone with limited experience such as myself.

I thought the issue may have been related to the directories, during my initial troubleshooting. At one point, I DID see an error about the destination directory "missing an operand" with the error message adding a "\r" to my directory. I didn't know what that was or where it came from until you just explained it. This was in a different portion of the script, but I assume it stemmed from the same underlying issue.

As frustrating as this was, I've learned a lot. Your help is again much appreciated.
User avatar
sander
Release Testers
Release Testers
Posts: 9255
Joined: January 22nd, 2008, 2:22 pm

Re: Script won't run, it's gotta be something simple

Post by sander »

(pre-)checking for windows-CRLF is easy:

Code: Select all

python3
Python 3.12.3 (main, Feb  4 2025, 14:48:35) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def contains_windows_CRLF(file):
...     with open(file, "rb") as fin:
...         all = fin.read()
...     windows_CRLF = b'\r\n' in all
...     return windows_CRLF
... 
>>> print(contains_windows_CRLF('/home/sander/sab-scripts/my-shell-pp-script.sh'))
False
>>> print(contains_windows_CRLF('/home/sander/sab-scripts/ugly-windows-line-endings.sh'))
True

EDIT

Ah, even shorter:

Code: Select all

>>> def contains_windows_CRLF(file):
...     with open(file, "rb") as fin:
...         return b'\r\n' in fin.read()
... 
>>> print(contains_windows_CRLF('/home/sander/sab-scripts/my-shell-pp-script.sh'))
False
>>> 
>>> print(contains_windows_CRLF('/home/sander/sab-scripts/ugly-windows-line-endings.sh'))
True
Post Reply