Is there any command in Linux through which i can know if the process is in hang state.
What do you mean by ‘hang state’? Typically, a process that is unresponsive and using 100% of a CPU is stuck in an endless loop. But there's no way to determine whether that has happened or whether the process might not eventually reach a loop exit state and carry on.
Desktop hang detectors just work by sending a message to the application's event loop and seeing if there's any response. If there's not for a certain amount of time they decide the app has ‘hung’... but it's entirely possible it was just doing something complicated and will come back to life in a moment once it's done. Anyhow, that's not something you can use for any arbitrary process.
Unfortunately there is no hung state for a process. Now hung can be deadlock. This is block state. The threads in the process are blocked. The other things could be live lock where the process is running but doing the same thing again and again. This process is in running state. So as you can see there is no definite hung state. As suggested you can use the top command ro see if the process is using 100% CPU or lot of memory.
Is there any command in Linux through which i can know if the process is in hang state.
There is no command, but once I had to do a very dumb hack to accomplish something similar. I wrote a Perl script which periodically (every 30 seconds in my case):
- run
ps
to find list of PIDs of the watched processes (along with exec time, etc) - loop over the PIDs
- start
gdb
attaching to the process using its PID, dumping stack trace from it usingthread apply all where
, detaching from the process - a process was declared hanged if:
- its stack trace didn't changed and time didn't change after 3 checks
- its stack trace didn't changed and time was indicating 100% CPU load after 3 checks
- hanged process was killed to give a chance a monitoring application to restart the hanged instance.
But that was very very very very crude hack, done to reach an about-to-be-missed deadline and it was removed few day later, after a fix for the buggy application was finally installed.
Otherwise, as all other responders absolutely correctly commented, there is no way to find whether the process hanged or not: simply because the hang might occur for way to many reasons, often bound to the application logic.
The only way is for application itself being capable of indicating whether it is alive or not. Simplest way might be for example a periodic log message "i'm alive".