i think you should refactor this into a function killwithtimeout pid  signal then in your code here  you would do something like if  killwithtimeout pid  sigterm  kwaitpidpolltimesforsigterm return true if  killwithtimeout pid  sigkill  kwaitpidpolltimesforsigkill return true in case of killing failure  log error timeout waiting for process pid to be killed  return false 