Python code to retrieve absolute path of tomcat service
Question: I am trying to write a python code that retrieves the absolute path of tomcat
service by searching through the services with the service name in linux. Is
there any module i can use, Code snipplets will be highly appreciated.

Thanks in advance.


Answer: `psutil` is probably what you're looking for.

<https://pypi.python.org/pypi/psutil>

    
    
    import psutil
    
    process_name = u'jboss'  # Place your case-insensitive process name here...
    interesting_processes = []
    
    # Loop over all processes, and see if process_name is in any segment of their command lines
    for process in psutil.get_process_list():
        try:  # You cannot access some processes unless you're root, so we try here
            for cmd_segment in process.cmdline:
                if process_name.lower() in cmd_segment.lower():
                    interesting_processes.append(process)
        except psutil.error.AccessDenied:
            continue  # processing other processes.
    
    # Show off our results
    for interesting_process in interesting_processes:
        print 'Process ID: {} has command: {}\n'.format(interesting_process.pid, u' '.join(interesting_process.cmdline))
    

In the above example, interesting_process.cmdline will be a list of arguments
which make up the command line of your application.

HTH

