python-pidfile icon indicating copy to clipboard operation
python-pidfile copied to clipboard

'cmd1 = psutil.Process(pid).cmdline()[0]' with IndexError: list index out of range

Open zhaopan-vip opened this issue 4 years ago • 1 comments

Exception:

File "... /pidfile/pidfile.py", line 29, in is_running
   cmd1 = psutil.Process(pid).cmdline()[0]
IndexError: list index out of range

This occurs when process been killed, with pidfile not removed... Then next time run it, the exception happens...

Should we change the cmdline check with code below?

        try:
            cmdline = psutil.Process(pid).cmdline()
            if not cmdline:
                return False
            cmd1 = cmdline[0]
            return cmd1 == self._process_name
        except psutil.AccessDenied:
            return False

zhaopan-vip avatar Oct 29 '21 08:10 zhaopan-vip

It is better to check the name of the program (Index 1) if the program was running and not only the interpreter? In venv Envoironment it is possible to start python3 as Interpreter and in a crontab only python interpreter without the "3". This resolves in a deletion of the pid-file

StefanZingler avatar Jul 04 '25 06:07 StefanZingler