# How to detect Hanged process in shell script?

**URL:** <https://community.unix.com/t/how-to-detect-hanged-process-in-shell-script/338030>\
**Category:** Shell Programming and Scripting\
**Created:** [December 24, 2013, 8:08am UTC](https://community.unix.com/t/how-to-detect-hanged-process-in-shell-script/338030 "2013-12-24T08:08:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nakul\_sh](https://community.unix.com/letter_avatar/nakul_sh/32/5_5575768a8748004e209b776fc1b2916d.png) [@Nakul\_sh](https://community.unix.com/u/Nakul_sh)\
**Post date:** [December 24, 2013, 8:08am UTC](https://community.unix.com/t/how-to-detect-hanged-process-in-shell-script/338030/1 "2013-12-24T08:08:35Z")

</div>

I have to check daily 20 processes each day. The names are like Network1 Network2 Network3 ....... Network20.  
There is built in utility for doing this. Following is the command to check a single network process.

```nohighlight
 check_process_status 1

```

If we want to check the status of Network2 then the commands is

```nohighlight
 check_process_status 2

```

If network process is running then this command gives a very long o/p.  
But if that process is hanged or having some problems then the command will not give any o/p. The session will remain ideal until I press Ctrl+C

How can find the network processes which are hanged. I need to get the process name for which

```nohighlight
 check_process_status 

```

dont give any o/p and gets stuck there. Checking for 20 processes daily is tedious job. Please help me

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/5_5575768a8748004e209b776fc1b2916d.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [December 24, 2013, 8:16am UTC](https://community.unix.com/t/how-to-detect-hanged-process-in-shell-script/338030/2 "2013-12-24T08:16:31Z")

</div>

Unless you give us more details, answers will be vague as well.  
You could run all checks in background, with output redirection, and check the output file size after some time. You also could check the process list for that 20 processes and see what state they are in to decide which are running and which are hung.

---

<div class="post-metadata">

**Author:** ![Nakul\_sh](https://community.unix.com/letter_avatar/nakul_sh/32/5_5575768a8748004e209b776fc1b2916d.png) [@Nakul\_sh](https://community.unix.com/u/Nakul_sh)\
**Post date:** [December 24, 2013, 8:31am UTC](https://community.unix.com/t/how-to-detect-hanged-process-in-shell-script/338030/3 "2013-12-24T08:31:31Z")

</div>

Thanks for your reply. While checking it manauly i do following. Lets assume i am checking Network2 process following is the command.

```nohighlight
check_process_status 2 

```

After entering the command i wait for 4-5 seconds. If the output (which is very long) doesn't come then i have to enter ctrl+c to come back to command prompt of UNIX.  
I want to find out for which processes this scenario is happening. i.e. I want to find out for which processes i would have pressed ctrl+c.

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/5_5575768a8748004e209b776fc1b2916d.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [December 24, 2013, 9:31am UTC](https://community.unix.com/t/how-to-detect-hanged-process-in-shell-script/338030/4 "2013-12-24T09:31:39Z")

</div>

Try this

```nohighlight
for p in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
do
 perl "alarm 15; exec @ARGV" check_process_status $p >/dev/null ||
 echo pkill Network$p
done

```

Remove the echo to really run the pkill.

---

<div class="post-metadata">

**Author:** ![Nakul\_sh](https://community.unix.com/letter_avatar/nakul_sh/32/5_5575768a8748004e209b776fc1b2916d.png) [@Nakul\_sh](https://community.unix.com/u/Nakul_sh)\
**Post date:** [December 24, 2013, 9:52am UTC](https://community.unix.com/t/how-to-detect-hanged-process-in-shell-script/338030/5 "2013-12-24T09:52:54Z")

</div>

Thanks for your reply.  
Just wanted to confirm: I am working in Ksh. So will that command work?  
alarm 15 will wait for 15 sec ?  
Also what is pkill command is used for? does it act like ctrl+c ?

Sorry but i am new to shell scripting so please help

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/5_5575768a8748004e209b776fc1b2916d.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [December 24, 2013, 12:26pm UTC](https://community.unix.com/t/how-to-detect-hanged-process-in-shell-script/338030/6 "2013-12-24T12:26:02Z")

</div>

In perl `alarm 15` will send a SIGALRM to itself after 15 seconds. It immediately execs into the check\_process\_status, so this will be killed!  
(While Ctrl-C sends SIGINT.)  
The pkill is for terminating the corresponding Network process.  
(kill and pkill send SIGTERM by default.)

---

<div class="post-metadata">

**Author:** ![Nakul\_sh](https://community.unix.com/letter_avatar/nakul_sh/32/5_5575768a8748004e209b776fc1b2916d.png) [@Nakul\_sh](https://community.unix.com/u/Nakul_sh)\
**Post date:** [December 24, 2013, 12:49pm UTC](https://community.unix.com/t/how-to-detect-hanged-process-in-shell-script/338030/7 "2013-12-24T12:49:07Z")

</div>

Thank you for such a good explanation... But that command is not working for unix server for my application. ☹  
Is it possible by using wait command?
