要在Linux上使用Python检查进程是否仍在运行,可以使用psutil
库。首先,需要安装psutil
库,可以使用以下命令安装:
pip install psutil
接下来,可以使用以下代码检查进程是否仍在运行:
import psutil
def is_process_running(process_name):
for process in psutil.process_iter(['name']):
if process.info['name'] == process_name:
return True
return False
process_name = 'your_process_name'
if is_process_running(process_name):
print(f"{process_name} is still running.")
else:
print(f"{process_name} is not running.")
请将your_process_name
替换为要检查的进程名称。如果进程仍在运行,将输出“进程名称 is still running.”,否则将输出“进程名称 is not running.”。
领取专属 10元无门槛券
手把手带您无忧上云