else #parent process { # This is the section I added before checking for # child's status and it solved the problem while () { printf ("Parent received from the child : $_\n"); } # thIs section checks and makes sure child process if Terminated # if its hung for more than 30 seconds. $count =0; $childprocess = qx (ps -ef|grep -v defunct|grep -v grep |grep $pid); while (($childprocess ne "") && ($count < 6)) { $count+=1; printf ("Found child process still running count=$count\n"); sleep (5); $childprocess = qx (ps -ef|grep -v defunct|grep -v grep |grep $pid); } if ($childprocess ne "") { qx (kill -9 $pid); printf (STDERR "Child ssh process hung. So forcibly killed it\n"); } waitpid($pid,0); # read the data printed by child while () { printf ("Parent received from the child : $_\n"); } }