How To Debug a Running Shell Script
am 31.03.2008 16:23:29 von simulant
Is it possible to debug a shell script that is already running? The
script was started with nohup and is running in the background.
According to the gdb docs, it looks like this can be done, but I
wanted to see if anyone has done this ...
From "http://sourceware.org/gdb/current/onlinedocs/gdb_5.html#SEC 18"
section 4.2:
**********
When `exec-wrapper' is set, the specified wrapper is used to launch
programs for debugging. GDB starts your program with a shell command
of the form exec wrapper program. Quoting is added to program and its
arguments, but not to wrapper, so you should add quotes if appropriate
for your shell. The wrapper runs until it executes your program, and
then GDB takes control.
You can use any program that eventually calls execve with its
arguments as a wrapper. Several standard Unix utilities do this, e.g.
env and nohup. Any Unix shell script ending with exec "$@" will also
work.
**********
Re: How To Debug a Running Shell Script
am 31.03.2008 17:53:35 von Icarus Sparry
On Mon, 31 Mar 2008 07:23:29 -0700, fraser.tim@gmail.com wrote:
> Is it possible to debug a shell script that is already running? The
> script was started with nohup and is running in the background.
>
> According to the gdb docs, it looks like this can be done, but I wanted
> to see if anyone has done this ...
>
> From "http://sourceware.org/gdb/current/onlinedocs/gdb_5.html#SEC 18"
> section 4.2:
>
> **********
> When `exec-wrapper' is set, the specified wrapper is used to launch
> programs for debugging. GDB starts your program with a shell command of
> the form exec wrapper program. Quoting is added to program and its
> arguments, but not to wrapper, so you should add quotes if appropriate
> for your shell. The wrapper runs until it executes your program, and
> then GDB takes control.
>
> You can use any program that eventually calls execve with its arguments
> as a wrapper. Several standard Unix utilities do this, e.g. env and
> nohup. Any Unix shell script ending with exec "$@" will also work.
> **********
gdb has no real facilities to debug shell scripts, running or otherwise.
gdb can help debug the shell, and you can examine the parse tree that is
being processed. To quote a song "This is like construction work with a
toothpick for a tool" to use as a method for debugging your script.
The section of the manual you quote is about starting a program.
Essentially it is talking about setting a "breakpoint" on the "execve"
system call, running until it hits the breakpoint, single stepping the
system call. Lots of quote marks deliberatly in the last sentence. I am
afraid this will not help you at all.
Re: How To Debug a Running Shell Script
am 31.03.2008 18:28:20 von Ed Morton
On 3/31/2008 9:23 AM, fraser.tim@gmail.com wrote:
> Is it possible to debug a shell script that is already running?
Only if you designed your script to be debugged while running. For example,
instead of:
while :
do
date=$(date)
echo "$date"
sleep 10
done
if you wrote something like:
debug() {
read debugLevel < debugFile
if [ "${debugLevel:-0}" -eq 0 ]; then
set +x
else
debugVar="$1"
eval debugVal="\$$debugVar"
printf "%s=%s\n" "$debugVar" "$debugVal" >&2
if [ "$debugLevel" -ge 2 ]; then
env >&2
fi
if [ "$debugLevel" -ge 3 ]; then
set -x
fi
# change this if you want more than one "hit", e.g.
# add an "iterations" field to the debugFile.
echo "0" > debugFile
fi
}
while :
do
date=$(date)
echo "$date"
debug "date"
sleep 10
done
then you just need to write whatever debugging level you like to "debugFile" and
the script will pick it up on it's next iteration.
Regards,
Ed.