def keep_alive(self):
        """
        """
        self._p0 = False

        self.handle_signals()

        with self.setup_output() as (stdout, stderr):

            self._terminating = False
            startretries = self.data.get('startretries', 3)
            initial_startretries = self.data.get('startretries', 3)

            while startretries:
                start = time.time()
                if startretries != initial_startretries:
                    log.info('Retry command (%i retries remain)' % startretries)
                env = os.environ.copy()
                update_env = {k:str(v) for (k, v) in self.data.get('env', {}).items()}
                env.update(update_env)
                cwd = self.data.get('cwd') or os.path.abspath(os.curdir)

                env_str = '\n'.join('\t%s: %r' % item for item in update_env.items())

                if env_str:
                    log.info("Setting Environment: \n%s" % env_str)

                if cwd:
                    log.info("Setting Working Directory: %s" % cwd)

                log.info("Running Command: %s" % ' '.join(self.data['command']))
                try:
                    self._p0 = Popen(self.data['command'],
                                     stdout=stdout, stderr=stderr,
                                     env=env, cwd=cwd, bufsize=self.data.get('bufsize', 0),
                                     preexec_fn=self.preexec_fn)
                except __HOLE__ as err:
                    log.exception('Program %s could not be started with popen' % self.name)
                    self.state.update(child_pid=None, exit_status=1,
                                      reason='OSError running command "%s"' % self.data['command'][0])
                    return
                except:
                    log.exception('Exception in keep_alive')
                    self.steate.update(child_pid=None, exit_status=1,
                                      reason='There was an unknown exception opening command (check logs)')
                    return

                log.info('Program started with pid %s' % self._p0.pid)
                self.state.update(child_pid=self._p0.pid, reason=None, exit_status=None,
                                  start_time=time.time())

                try:
                    status = self._p0.wait()
                except KeyboardInterrupt:
                    log.error('Program %s was interrupted by user' % self.name)
                    kill_tree(self._p0.pid)
                    self.state.update(child_pid=None, exit_status=None, reason='Interrupted by user')
                    raise
                except BaseException as err:
                    log.error('Program %s was interrupted' % self.name)
                    kill_tree(self._p0.pid)
                    self.state.update(child_pid=None, exit_status=None, reason='Python BaseException')
                    log.exception(err)
                    raise

                self._p0 = False

                uptime = time.time() - start

                log.info('Command Exited with status %s' % status)
                log.info(' + Uptime %s' % uptime)

                if self._terminating:
                    reason = "Terminated at user request"
                    status = None
                elif status in self.data['exitcodes']:
                    reason = "Program exited gracefully"
                elif uptime < self.data['startsecs']:
                    reason = 'Program did not successfully start'
                    startretries -= 1
                else:
                    reason = "Program exited unexpectedly with code %s" % (status)
                    startretries = initial_startretries

                self.state.update(child_pid=None, exit_status=status,
                                  reason=reason)

                if self._terminating:
                    break
                if status in self.data['exitcodes']:
                    break

        log.debug("Exiting keep alive function")
dataset/ETHPy150Open Anaconda-Platform/chalmers/chalmers/program/base.py/ProgramBase.keep_alive