Runs code periodically allowing a graceful shutdown