attaching a shell script
Is there any debugging tool, which can be used to attach and debug a running shell script?
I am using RedHat Distro. I have a shell script which is working properly, but at times the behaviour is undesirable. I am unable to find out what is happening to the script? Any Help is appreciated. TIA. |
Using the -v option will print lines as they are read. There is also --debug.
http://bashdb.sourceforge.net/ Tips on debugging bash scripts can be found HERE |
echo and lots of patients
|
You could also try bash -x scriptname .
|
The problem with any of the verbose modes is that the script will display all the lines in your script even ones that are not part of the execution flow, if your script is at all complex it becomes difficult to make sense of the output.
|
The output that results from using the -v and -x flags goes to standard error so you can redirect it to a file and analyze it after the run, ie
bash -vx myscript 2>myscript.log |
My point is that simply using -v and -x gives you info that can be misleading.
I have found echo to be more useful when things get a bit tricky. |
If you have an idea which section is failing, you can:
set -xv just before it, then set +xv just after to turn it off |
all,
Thanks for the replies. But my problem is a bit different. All the options suggested, requires the code to be changed or the script to be started with the necessary arguments. My script is started at the time of PC boot itself and runs forever. At the begining it is ok, but after 2 or 3 days, it starts misbehaving. So i want to attach the script while there is a problem. What i want is something like gdb, which can be used to attach a C executable and debug it. I have searched the net for this and found no options. I belive no counterpart of gdb for shell script is available. I am going to try the echos as well as the -x option redirecting to a file. Meanwhile, any suggestions is always welcome. TIA. |
i wanna add te comments to the prev.
The pseudo code of the script is like a=29 while : do Read a file. Init a variable from it. Perform actions based on the variable. read from a auto mounted file system of another PC. Do some actions sleep $a done My assumption is that this loop has to run only every 29 seconds. But wat i see is that, the loop is done continuosly, as if the sleep itself is not there. This phenomenon occurs only after running for 2 or 3 days. And also only in one PC. In another PC, i am able to run this script without problem for many days. Is there any way the variable $a can be corrupted? Or wat could be the cause for such a thing to happen? This problem is bugging me for last several days. Any help is very much appreciated. TIA. |
Your problem is that you are opening the file without closing it again
this results in a new file descriptor being open every 29 secs eventually you run out of resources a=29 while do Read a file. Init a variable from it. Perform actions based on the variable. read from a auto mounted file system of another PC. Do some actions sleep $a done |
dommy,
Wat i gave was just a pseudo-code. I am just using cat command and grepping from it. So there is no need for explicit closes. I wonder if there is any open and close possible from shell script. Also, if we run out of resources, i think, the kernel will remove the process from execution. I am not sure though. |
Quote:
[n]<> [Filename] #Opens Filename for read/write as file descriptor n. [n]< [Filename] #Opens Filename for read only access as file descriptor n. [n]> [Filename] #Opens Filename for write access as file descriptor n. EXAMPLE: (From Advanced Bash Scripting Guide) Code:
echo 1234567890 > File # Write string to "File". I would suggest you read the link I posted on bash troubleshooting tips. You should be able to add a few lines to the script to figure out the problem. It could be that the script is relying on some global system variable that is getting borked by another process? |
All times are GMT -5. The time now is 10:34 PM. |