4

I was in the same place as this guy where I thought a process had caused bash to hang, but instead it was just taking a while. I ran gem install berkshelf, and fortunately, the process showed up on Activity Monitor, so I knew it was still going, but is there a more reliable way to do this?

1
  • I use top to see whether it's doing anything. If the process is maxed out a 99% for a long while, it's quite probably in an infinite loop and should be killed. If it's sitting at 0% then probably waiting for input from somewhere. Compare it to other processes and how much CPU time they're using and how the usage fluctuates, and you should be able to get a feel for it after a while.
    – pdah
    Jan 20 '14 at 17:54
3

You actually cannot tell the difference between a process hanging or "just taking a while". A process can respond to signals, but does not have to.

When a process "hangs", it probably got stuck in an infinite loop, or is waiting for some event that never happens. But it could also just do a lot of work.

For example, a process that does intensive work might not immediately react to Ctrl-C (a SIGINT termination signal), but could exit a little later, or not at all. That's usually the case where you send a SIGKILL, which cannot be ignored by a process.

Note that in this whole situation, the actual shell (e.g., Bash) is never hanging. Simply speaking, the shell just executes the process and puts it into the foreground. You could still suspend the process and continue running it in the background (Ctrl-Z, then bg). Your shell would still work.

2
  • I know a little about <kbd>Ctrl-C</kbd>, and if I press it twice, the process can't ignore it. Is pressing <kbd>Ctrl-C</kbd> twice the SIGKILL you're talking about? Jan 19 '14 at 19:56
  • 2
    No, the Ctrl-C key combination only sends SIGINT, never a SIGKILL. Hitting it twice usually doesn't do anything different – but there are some particularities. See: Why does gnu readline require me to hit control c twice?. The SIGKILL can only be sent with kill(1), by calling kill -9 <pid>.
    – slhck
    Jan 19 '14 at 20:00
0

A good thing to do is to run processes that take a long time in verbose mode. That way you can see progress and if it hangs, you know where and can (hopefully) resolve the problem. Also, (by default, with stty sane) Ctrl+C sends SIGINT, but Ctrl+Z sends SIGTSTP.

3
  • Awesome! Once I get some reputation, I'll be sure to upvote this. How do you run in verbose mode? something like -v right? would I do gem -v install berkshelf? Jan 19 '14 at 20:14
  • It is usually -v, but if not you can try to read the man pages on the command or do -h (help) to find out the flag for verbose. Some commands will even have a very verbose mode.
    – Lattis
    Jan 19 '14 at 20:17
  • 1
    Ctrl-Z does not send SIGKILL, it sends SIGSTP. You cannot send SIGKILL through normal Bash keyboard shortcuts.
    – slhck
    Jan 19 '14 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.