Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto debug running bash script

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.

like image 414
long Avatar asked Feb 05 '26 08:02

long


1 Answers

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.

like image 141
hesham_EE Avatar answered Feb 07 '26 01:02

hesham_EE