Debugging shell scripts, User Interfaces and compiled programs on Linux

When I want to debug a shell/bash shell script, I add the following to the script:


set -x
export PS4='+(${BASH_SOURCE}:${LINENO}): '

Other times, I export PS4 in my interactive shell, and run the script with “bash -x /path/to/script”. More bash script debugging tips are here: http://wiki.bash-hackers.org/scripting/debuggingtips

When I want to see what commands are being run on a Linux machine by a higher-level user interface, I turn to startmon. It shows me every process created, along with its arguments. This can be useful for figuring out how a CD/DVD-creation program calls wodim/cdrecord, or how it mounts a drive.

When I don’t have source code to a compiled program, and I want to see what system calls it is making, and check into why it might be failing to run, I use strace.

When I want to find out which programs are preventing me from unmounting a DVD or a USB flash drive, I use ‘lsof‘, which is available for nearly every UNIX-like operating system.

When I want to know what programs are consuming the most disk I/O and making things go slow, I use ‘top’ to see the top-memory consumers, and whether they have a high number of page faults. I use ‘iotop’ as well.