3

I have a bash script running on Ubuntu. Is it possible to see the line/command executed now without script restart.

The issue is that script sometimes never exits. This is really hard to reproduce (now I caught it), so I can't just stop the script and start the debugging.

Any help would be really appreciated

P.S. Script logic is hard to understand, so I can't to figure out why it's frozen by power of thoughts.

10
  • This is answered here: Bash scripting, checking for errors, logging You can use the ">>" operator to insert commands which make entries into your error_log file. Commented Sep 23, 2014 at 12:58
  • 2
    The normal way to debug a shell script is to add set -x to the beginning, but that will require restarting it. The only suggestion I can think of is to use strace, but I don't know how easy it will be to translate its output into the script lines. Commented Sep 23, 2014 at 12:58
  • 2
    @FranzHolzinger How would he do that without restarting the script? Commented Sep 23, 2014 at 12:59
  • Of course the script must either have already generated somehow the output. Or the output commands must be inserted and the script restarted. Commented Sep 23, 2014 at 13:01
  • 1
    possible duplicate of A running bash script is hung somewhere. Can I find out what line it is on? Commented Sep 23, 2014 at 13:27

1 Answer 1

3

Try to find the process id (pid) of the shell, you may use ps -ef | grep <script_name> Let's set this pid in the shell variable $PID. Find all the child processes of this $PID by:

ps --ppid $PID

You might find one or more (if for example it's stuck in a pipelined series of commands). Repeat this command couple of times. If it doesn't change this means the script is stuck in certain command. In this case, you may attach trace command to the running child process:

sudo strace -p $PID

This will show you what is being executed, either indefinite loop (like reading from a pipe) or waiting on some event that never happens.

In case you find ps --ppid $PID changes, this indicates that your script is advancing but it's stuck somewhere, e.g. local loop in the script. From the changing commands, it can give you a hint where in the script it's looping.

Sign up to request clarification or add additional context in comments.

3 Comments

Well, your approach gave me at least something. code sudo strace -p $PID code produce code read(3, code . Digit is file descriptor, using lsof I found that it's unix socket. So, script call for some system operation and wait for response from it using that socket. Unfortunately, that's not enough.
1- What is command that this script is stuck at? 2- Could you share the output of cat /proc/${PID}/fdinfo/3
Unfortunately, bug is really rare ... I'l try to run the command next time it reproduces

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.