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 using thread apply all where
, detaching from the process
- a process was declared hung if:
- its stack trace didn't change and time didn't change after 3 checks
- its stack trace didn't change and time was indicating 100% CPU load after 3 checks
- hung process was killed to give a chance for a monitoring application to restart the hung instance.
But that was very very very very crude hack, done to reach an about-to-be-missed deadline and it was removed a few days 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 hung 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".