SABnzbd

Wiki

User Manual FAQ Contact Introduction Installation Configuration Scripts Advanced Topics Extensions for SABnzbd

Incorrect or missing information? Post-processing scripts

This Wiki page is for an older version of SABnzbd.
You can download the most recent stable version (SABnzbd 4.2.2) on our downloads page.

SABnzbd can run a user-provided post-processing scripts. The scripts can be associated with a job entry when adding the job manually, via category or during downloading. Scripts can also be run as an end-of-queue event. A post-processing script will only run when the job has completed without errors. If you want to always run the script, you need to disable the option Post-Process Only Verified Jobs.

Scripts may use any scripting language available on your system; common choices are Python, Unix shell, and Windows batch scripts. All scripts must be located in the Scripts-directory, specified in Config->Folders and must be executable. On Unix-like operating systems (Linux, BSD, etc.) this means the x-bit must be on. On Windows, the requirement is that the script's extension is listed in your system's PATHEXT environment variable.

The console output of the script is captured by SABnzbd and is saved in the so called "Script Log" which can be opened from the history details. The output will be added to the (optional) notification email and can be examined in the History page.

The "return" code that your script itself returns should be 0 if all went well or non-zero if errors occurred. If the return code is something else than 0, SABnzbd's History will show that return code (also known as exit code) as Exit(...) in the History. By default the job will not be flagged as failed in case the exit code is non-zero. In Config->Switches there is the option to let a non-zero code mark the job as failed.

Example scripts are included in the scripts directory of your SABnzbd installation and are shown at the end of this page.


Basic parameters

The script will receive the parameters described below. Use %1 in Windows scripts and $1 in Unix scripts. Note that on Windows the input parameters are surrounded by quotes (e.g. "job name").

NOTE Much more information is available to scripts via environment variables, see below!

Position Description
1 The final directory of the job (full path)
2 The original name of the NZB file
3 Clean version of the job name (no path info and ".nzb" removed)
4 Indexer's report number (if supported)
5 User-defined category
6 Group that the NZB was posted in e.g. alt.binaries.x
7 Status of post processing.
  • 0 = OK
  • 1 = Failed verification
  • 2 = Failed unpack
  • 3 = 1+2
  • -1 = Failed post processing
8 URL to be called when job failed (if provided by the server, it is always sent, so check parameter 7!).
The URL is provided by some indexers as the X-DNZB-Failure header

Environment variables

Your script can get extra information via environment variables:

Variable Description
SAB_SCRIPT The name of the current script
SAB_NZO_ID The unique ID assigned to the job
SAB_FINAL_NAME The name of the job in the queue and of the final folder
SAB_FILENAME The NZB filename (after grabbing from the URL)
SAB_COMPLETE_DIR The whole path to the output directory of the job
SAB_PP_STATUS Was post-processing successfully completed (repair and/or unpack, if enabled by user)
SAB_CAT What category was assigned
SAB_BYTES Total number of bytes
SAB_BYTES_TRIED How many bytes of the total bytes were tried
SAB_BYTES_DOWNLOADED How many bytes were received (can be more than tried, due to overhead)
SAB_DUPLICATE Was it detected as duplicate
SAB_UNWANTED_EXT Were there unwanted extensions
SAB_OVERSIZED Was the job over the user's size limit
SAB_PASSWORD What was the password supplied by the NZB or the user
SAB_CORRECT_PASSWORD The password that was detected as being the correct password for this job (added in 3.4.0)
SAB_ENCRYPTED Was the job detected as encrypted
SAB_STATUS Current status (completed/failed/running)
SAB_FAIL_MSG If job failed, why did it fail
SAB_AGE Average age of the articles in the post
SAB_URL URL from which the NZB was retrieved
SAB_AVG_BPS Average bytes/second speed during active downloading
SAB_DOWNLOAD_TIME How many seconds did we download
SAB_PP What post-processing was activated (download/repair/unpack/delete)
SAB_REPAIR Was repair selected by user
SAB_UNPACK Was unpack selected by user
SAB_FAILURE_URL Provided by some indexers as alternative NZB if download fails
SAB_PRIORITY Priority set by user
SAB_GROUP Newsgroup where (most of) the job's articles came from
SAB_ORIG_NZB_GZ Path to the original NZB-file of the job. The NZB-file is compressed with gzip (.gz)
SAB_VERSION The version of SABnzbd used
SAB_PROGRAM_DIR The directory where the current SABnzbd instance is located
SAB_PAR2_COMMAND The path to the par2 command on the system that SABnzbd uses
SAB_MULTIPAR_COMMAND Windows-only (empty on other systems). The path to the MultiPar command on the system that SABnzbd uses
SAB_RAR_COMMAND The path to the unrar command on the system that SABnzbd uses
SAB_ZIP_COMMAND The path to the unzip command on the system that SABnzbd uses
SAB_7ZIP_COMMAND The path to the 7z command on the system that SABnzbd uses. Not all systems have 7zip installed (it's optional for SABnzbd), so this can also be empty
PYTHONUNBUFFERED This variable is set to 1 when running a Python script (.py), in order to force Python to write output to SABnzbd directly instead of buffering it

Example Python script using basic parameters

The get the parameters in python, you can do this:

import sys
try:
    (scriptname, directory, orgnzbname, jobname, reportnumber, category, group, postprocstatus, url) = sys.argv
except:
    print("No commandline parameters found")
    sys.exit(1)

# continue script

# Your code goes here

# Success code
sys.exit(0)

Example Python script using environment variables

import os

print("SABnzbd version:", os.environ['SAB_VERSION'])
print("Job location:", os.environ['SAB_COMPLETE_DIR'])
print("Fail msg:", os.environ['SAB_FAIL_MSG'])

# Your code

# Success code
sys.exit(0)