Example #1
0
 def test_process_cmdline(self):
     pid = self.createProcess()
     self.assertRegex(tools.process_cmdline(pid),
                      r'.*/sh.*/common/test/dummy_proc\.sh')
     self.killProcess()
     pid = self.createProcess('foo', 'bar')
     self.assertRegex(tools.process_cmdline(pid),
                      r'.*/sh.*/common/test/dummy_proc\.sh.foo.bar')
    def test_existing_process_with_correct_proc_cmdline(self):
        """
        Test the check function with an existing process with correct process
        cmdline (for backwards compatibility)
        """
        pid = self.createProcess()
        procname = tools.process_cmdline(pid)

        # create file with pid and process name
        with open(self.file_name, "wt") as file_with_pid:
            file_with_pid.write(str(pid) + "\n")
            file_with_pid.write(procname)

        # Execute test
        self.assertFalse(self.inst.check())
    def check(self, auto_exit=False):
        """
        Check if the current application is already running

        Args:
            auto_exit (bool):   automatically call sys.exit if there is an other
                                instance running

        Returns:
            bool:               True if this is the only application instance
        """
        # check if the pidfile exists
        if not os.path.isfile(self.pid_file):
            return True

        self.pid, self.procname = self.readPidFile()

        # check if the process with specified by pid exists
        if 0 == self.pid:
            return True

        if not tools.is_process_alive(self.pid):
            return True

        # check if the process has the same procname
        # check cmdline for backwards compatibility
        if (
            self.procname
            and self.procname != tools.process_name(self.pid)
            and self.procname != tools.process_cmdline(self.pid)
        ):
            return True

        if auto_exit:
            # exit the application
            print("The application is already running !")
            exit(0)  # exit raise an exception so don't put it in a try/except block

        return False