kill 0, $pid while sleep 1 and $timeout--; kill 3, $pid if $timeout; #### sleep 1 while kill(0, $pid) && $timeout--; kill 3, $pid if $timeout;