21

Using the command 'top' I can see 2 python scripts are running. However, how do I check their names or directory/location? I want to identify them so I can see what is running properly and what isn't.

Anthon
  • 79,293
User
  • 329
  • 1
    lsof -p $PID would be a good start. $PID can also be a comma-delimited list of PIDs. Also, tons of data will be exposed in /proc/$PID/. – DopeGhoti Jan 24 '14 at 04:43

3 Answers3

36

You can get a list of python processes using pgrep:

pgrep -lf python

This, however, does not list the whole command line. If you have a recent version of pgrep you can use -a to do this:

pgrep -af python

Otherwise, you can use /proc:

IFS=" " read -ra pids < <(pgrep -f python)
for pid in "${pids[@]}"; do
    printf '%d: ' "$pid"
    tr '\0' ' ' < "/proc/$pid/cmdline"
    echo
done
Chris Down
  • 125,559
  • 25
  • 270
  • 266
11

I usually use ps -fA | grep python to see what processes are running.

This will give you results like the following:

UID     PID   PPID  C STIME TTY      TIME     BIN            CMD
user    3985  3960  0 19:46 pts/4    00:00:07 path/to/python python foo.py

The CMD will show you what python scripts you have running, although it won't give you the directory of the script.

blaklaybul
  • 211
  • 2
  • 3
1
import psutil

def check_process_status(process_name):
    """
    Return status of process based on process name.
    """
    process_status = [ proc for proc in psutil.process_iter() if proc.name() == process_name ]
    if process_status:
        for current_process in process_status:
            print("Process id is %s, name is %s, staus is %s"%(current_process.pid, current_process.name(), current_process.status()))
    else:
        print("Process name not valid", process_name)