1

Bash randomly hangs for a few seconds after executing a command. At that stage, Ctrl+C or anything just gets printed on the terminal.
I use tmux, so I had already opened up htop to view resource usage in a different pane. All was fine. Also, when it is hung, moving to other panes works fine, I can type anything, but it hangs up when I press Enter. All of the panes execute as soon as the first pane which was hung continues. The point being resources are not a bottleneck.

I tried to use set -x in bash to check if something is happening, but due to unpredictable hangs, I had to revert it back to keep working sanely.

I also looked up How to profile a bash shell script slow startup?, but I can't find a way in there, to find slow bash after executing a command.

Any ideas how do I debug it? I am slowly going mad.

Edit: To add more context, the hang varies from 5 seconds to a minute.
To give an example, consider this:

$ ls
<--- hangs here
// after a few seconds, it continues
file1.txt
<--- never hangs here, after executing the cmd
$

And this is just an example, nothing to do with ls. I find no correlation to any particular command by which I can reproduce this.

subtleseeker
  • 4,415
  • 5
  • 29
  • 41
  • Have you looked at the kernel ring buffer (dmesg)? – Raman Sailopal Oct 07 '20 at 11:09
  • I strongly adivce you to parse your initfiles with `shellcheck` and check for errors. Secondly, avoid any command execution which are irrelevant. – kvantour Oct 07 '20 at 11:30
  • @RamanSailopal I looked up what dmesg is. So from what I understand, it is filled up at system boot. I don't understand how that relates to my problem...To give more context, I face this problem on remote server which is rebooted quite infrequently (months). – subtleseeker Oct 07 '20 at 12:25
  • @kvantour Just to be sure, I passed my bashrc through shellcheck. Nothing too major. But as I said the problem comes soon after I execute a command and not before that command is completed. Not sure how this helps. – subtleseeker Oct 07 '20 at 12:32
  • @subtleseeker dmesg outputs messages direct from the kernel. It isn't just populated at boot time. If there are any errors encountered by the kernel as a result of the script execution (and possibly causing the hang), it will be shown in dmesg. – Raman Sailopal Oct 07 '20 at 13:00
  • while I understand the behavior of `ls` has been provided as an example, I'd be curious as to what other commands are hanging and what they might have in common, eg, are all (hanging) commands being run against a network mounted FS? – markp-fuso Oct 07 '20 at 14:08
  • I would suggest running an `strace` of your shell and see what syscall it hangs on when you run `ls`. I suspect something in your search path takes a while to scan. – B. Morris Oct 11 '20 at 20:58
  • @B.Morris Hey, as I already told in the question, its not specific to `ls`, it was an example. I can't find any correlation to when this happens. I can't see anything related to a particular network partition. I can't reproduce it either. It happens say 10 times in an hour. In short, I need to log behind the scenes to check what went wrong only after it happens. Do you suggest to run strace in background? If so, should I use any particular filters? – subtleseeker Oct 12 '20 at 04:53
  • If it takes up to a minute to recover, and you're able to get to another shell quickly, you could even start an `strace` on it after it's hung; you'll see the hung call as the first call in `strace` (though in some cases it might not show up until it actually exits after the delay). Without knowing for sure what is causing the delay, it's hard to say what filters to use. If you do end up running it in the background, you could periodically restart it if the output gets too big. – B. Morris Oct 12 '20 at 04:57
  • 2023 And I've been seeing this and wondering too. Ubuntu 22.04 with KDE Plasma 5.24.7 ... using a small custom .cpp.built with g++-11 (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0. – capella Sep 03 '23 at 03:07

0 Answers0