1

Is there any way to run a process with a customized name like labeling it? so if i want to kill the process it will be easy to find it with that specific name or label.

Here is my code that find any process by a given name:

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

For example if i tried to run my application:

python manage.py runserver -h 192.168.0.110 -p 5050

If I tried to find that running process PID by the process name 'python' it will gives me yet another processes as it shows under all the process name are 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
   }
]

By running bash -c "exec -a myapp python manage.py runserver -h 192.168.0.110" it gives this error:

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

1 Answers1

-3

What environment are you running this on? You may be able to use this previous answer to start your process with a specific name

PygoNode
  • 90
  • 5