Shell Script Debugging for Beginners

Running FileBot from the console, Groovy scripting, shell scripts, etc
Post Reply
User avatar
rednoah
The Source
Posts: 22856
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Shell Script Debugging for Beginners

Post by rednoah »

It's easy to debug shell scripts when you can call them manually in the console and see what happens, but in many cases your script is called by another program (e.g. cron, Task Scheduler, Automator, etc).



How to debug scripts that are called by other tools?

If your script is called by another tool, you won't see any of the output. If something fails, or isn't working, the first thing is to add IO Redirection to all the calls to redirect the console output to a file.

It's simple:
  • Make sure your script is actually called
  • Make sure to collect all the output so you can see what's going on

1.
First we make sure that our script is actually called, by adding simple debugging statements like this:

Shell: Select all

printenv > "/path/to/log.txt"
By making a simple call that can't fail, we confirm that the script is called, and that we have file permissions to our log file. Make sure that the user running your script does have write permissions for your log location.


2.
Then we add IO redirection to the call that doesn't work:

Shell: Select all

filebot -script fn:sysenv > "/path/to/log.txt" 2>&1
At this point we know it's called, so it must error out for some reason. Make sure to collect BOTH standard output and standard error output.


3.
Please read the debug.sh example script.



Step-by-Step Instructions

  1. Download the debug.sh script:

    Shell: Select all

    curl -O "https://raw.githubusercontent.com/filebot/plugins/master/bash/debug.sh"
  2. Read the debug.sh script and try to understand what it does line by line:

    Shell: Select all

    cat debug.sh
  3. Make the debug.sh script executable for all users:

    Shell: Select all

    chmod +rx debug.sh
  4. Enter the absolute path to your /path/to/debug.sh script to execute it:

    Shell: Select all

    /path/to/debug.sh
  5. Read the console output for each test command (e.g. printenv > /tmp/printenv.txt) and confirm that each test command is working:

    Shell: Select all

    tail /tmp/*.txt
  6. Copy the absolute path to your /path/to/debug.sh script into qBT / Deluge / Transmission / etc and wait for it to be called. Then check the /tmp/*.txt text files to see what the console output (i.e. error messages) was for each test command.



The FileBot Debug Script

If the problem is difficult to pin down, then we can write our own filebot-debug.sh command, and exchange the filebot command with or own filebot-debug.sh command, that behaves exactly as if it was the filebot command by calling filebot internally, but with lots of custom debug output before and after, and redirecting all standard output and error output, so that we can see exactly how filebot is called and what filebot is doing, even if filebot itself crashes on startup or during runtime.

e.g. debug qBT integration

Shell: Select all

/path/to/filebot-debug.sh @/path/to/args.txt --def "ut_label=%L" "ut_state=%S" "ut_title=%N" "ut_kind=%K" "ut_file=%F" "ut_dir=%D"
:idea: Please read the FAQ and How to Request Help.
Post Reply