1

有什么方法可以运行一个自定义名称的进程,比如给它贴标签?因此,如果我想终止该进程,使用该特定名称或标签很容易找到它。

这是我通过给定名称查找任何进程的代码:

def findProcessIdByName(processName):
    '''
    findProcessIdByName('python')
    Get a list of all the information of a specific process
    '''

    listOfProcessObjects = []

    #Iterate over all the running process
    for proc in psutil.process_iter():
       try:
           pinfo = proc.as_dict(attrs=['pid', 'name', 'create_time'])
           # Check if process name contains the given name string.
           # pinfo['path'] = psutil.Process(pinfo['pid']).cmdline()
           if processName.lower() in pinfo['name'].lower() :
               listOfProcessObjects.append(pinfo)
       except (psutil.NoSuchProcess, psutil.AccessDenied , psutil.ZombieProcess) :
           pass

    return listOfProcessObjects

例如,如果我尝试运行我的应用程序:

python manage.py runserver -h 192.168.0.110 -p 5050

如果我试图通过进程名称“python”找到正在运行的进程 PID,它将给我另一个进程,因为它在所有进程名称下显示都是python

findProcessIdByName('python')

# Results
[ 
   { 
      'create_time':1571059481.29,
      'name':'python',
      'pid':9441
   },
   { 
      'create_time':1571059730.89,
      'name':'python',
      'pid':9889
   },
   { 
      'create_time':1571061314.85,
      'name':'python',
      'pid':13297
   },
   { 
      'create_time':1571062673.5,
      'name':'python',
      'pid':17716
   },
   { 
      'create_time':1571062674.62,
      'name':'python',
      'pid':17721 # This one is that running process
   }
]

通过运行bash -c "exec -a myapp python manage.py runserver -h 192.168.0.110"它给出了这个错误:

Traceback (most recent call last):
  File "manage.py", line 3, in <module>
    import werkzeug.serving
ModuleNotFoundError: No module named 'werkzeug'
4

1 回答 1

-2

你在什么环境下运行这个?您也许可以使用此先前的答案以特定名称开始您的流程

于 2019-10-14T15:55:10.703 回答