Check whether a process is running This function takes the process ID or pid and checks whether it is running. It works for Windows and UNIX-like systems. Return True or False
(pid)
| 79 | |
| 80 | |
| 81 | def process_running(pid): |
| 82 | """Check whether a process is running |
| 83 | |
| 84 | This function takes the process ID or pid and checks whether it is |
| 85 | running. It works for Windows and UNIX-like systems. |
| 86 | |
| 87 | Return True or False |
| 88 | """ |
| 89 | if os.name == "nt": |
| 90 | # We are on Windows |
| 91 | process = subprocess.Popen(["tasklist"], stdout=subprocess.PIPE) |
| 92 | output, _ = process.communicate() |
| 93 | lines = [line.split(None, 2) for line in output.splitlines() if line] |
| 94 | for name, apid, _ in lines: |
| 95 | name = name.decode("utf-8") |
| 96 | if name == EXEC_MYSQLD and pid == int(apid): |
| 97 | LOGGER.debug("Process %d is running.", pid) |
| 98 | return True |
| 99 | LOGGER.debug("Process %d not running.", pid) |
| 100 | return False |
| 101 | |
| 102 | # We are on a UNIX-like system |
| 103 | try: |
| 104 | os.kill(pid, 0) |
| 105 | except OSError: |
| 106 | return False |
| 107 | return True |
| 108 | |
| 109 | |
| 110 | def process_terminate(pid): |
no test coverage detected
searching dependent graphs…