what is the purpose of waiting in case if need to wait until program exits  i recommend next solution local kill if  pid then return fi kill pid dev null for i in  seq nailgun start max wait time do if kill pid dev null then kill sleep else kill break fi done if kill ne then kill pid dev null fi return  