Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python library for Linux process management [closed]

Through my web interface I would like to start/stop certain processes and determine whether a started process is still running.

My existing website is Python based and running on a Linux server, so do you know of a suitable library that supports this functionality?

Thanks

like image 347
hoju Avatar asked Nov 10 '09 01:11

hoju


People also ask

What is psutil?

psutil (process and system utilities) is a cross-platform library for retrieving information on running processes and system utilization (CPU, memory, disks, network, sensors) in Python. It is useful mainly for system monitoring, profiling and limiting process resources and management of running processes.

How do you find the process status in Python?

You could use a the status feature from psutil: import psutil p = psutil. Process(the_pid_you_want) if p. status == psutil.

How Process management is done in Linux?

The operating system tracks processes through a five-digit ID number known as the pid or the process ID. Each process in the system has a unique pid. Pids eventually repeat because all the possible numbers are used up and the next pid rolls or starts over.


5 Answers

To start/stop python sub processes you can use the subprocess module. To check whether they are running you might use psutil:

>>> import psutil
>>> pid = 1034  # some pid
>>> psutil.pid_exists(pid)
True
>>>

...or this (it will also check if the PID has been reused):

>>> p = psutil.Process(pid)
>>> p.is_running()
True
>>>
like image 80
Giampaolo Rodolà Avatar answered Nov 15 '22 14:11

Giampaolo Rodolà


Checking the list of running processes is accomplished (even by core utilities like "ps") by looking at the contents of the /proc directory.

As such, the library you're interested for querying running processes is the same as used for working with any other files and directories (i.e. sys or os, depending on the flavor you're after. Pay special attention to os.path though, it does most of what you're after). To terminate or otherwise interact with processes, you send them signals, which is accomplished with os.kill. Finally, you start new processes using os.popen and friends.

like image 29
tylerl Avatar answered Nov 15 '22 14:11

tylerl


Since you said this is a Linux server, calling the external ps binary is usually slower, uses more resources and is more error prone than using the information from /proc directly.

Since nobody else mentioned, one simple way is:

glob.glob('/proc/[0-9]*/')

Good luck.

like image 43
Yves Junqueira Avatar answered Nov 15 '22 15:11

Yves Junqueira


This is what i use. It uses procfs (so you are limited to Unix like systems, will not work on macs i think) and the previously mentioned glob. It also gets the cmdline, which allows you to identify the process. For killing the process you can use os.kill(signal.SIGTERM, pid). For using subprocess, please check this post Python, Popen and select - waiting for a process to terminate or a timeout

def list_processes():
    """
    This function will return an iterator with the process pid/cmdline tuple

    :return: pid, cmdline tuple via iterator
    :rtype: iterator

    >>> for procs in list_processes():
    >>>     print procs
    ('5593', '/usr/lib/mozilla/kmozillahelper')
    ('6353', 'pickup -l -t fifo -u')
    ('6640', 'kdeinit4: konsole [kdeinit]')
    ('6643', '/bin/bash')
    ('7451', '/usr/bin/python /usr/bin/ipython')
    """
    for pid_path in glob.glob('/proc/[0-9]*/'):

        # cmdline represents the command whith which the process was started
        f = open("%s/cmdline" % pid_path)
        pid = pid_path.split("/")[2] # get the PID
        # we replace the \x00 to spaces to make a prettier output from kernel
        cmdline = f.read().replace("\x00", " ").rstrip()
        f.close()

        yield (pid, cmdline)
like image 29
Darjus Loktevic Avatar answered Nov 15 '22 13:11

Darjus Loktevic


The os module is probably your friend. There's os.kill, for instance to kill a process.

In terms of getting a list of processes, you'll probably want to shell out to the ps command. This question has more information on that.

like image 37
Schof Avatar answered Nov 15 '22 13:11

Schof