본문 바로가기

Language&Configuration/[Liunx]

[Linux] ps 명령어 사용법 - 실행 중인 프로세스 확인하기

1. 개요

ps 명령어는 리눅스에서 현재 실행중인 프로세스를 확인하는 명령어 입니다.
Process Status 이름 그대로 명령어를 실행하면 현재 실행되고 있는 프로세스들의 정보를 화면에 출력합니다.

  • 윈도우 : 특정 프로세스가 실행 중인지 확인하거나 강제 종료하기 위해 작업 관리자를 사용
  • 리눅스 : ps 명령어가 사용

 

2. 사용 용도

  • GUI를 사용하지 않는 서버 환경에서 특정 프로세스가 동작 중인지 확인(대부분의 프로세스들이 백그라운드에서 동작하기 때문에)
  • bash 스크립트(script)를 통한 자동화에도 자주 사용
  • 주로 특정 프로세스에 시그널(signal)을 보내야 할 때 PID(process id)를 식별하기 위해 사용
  • 프로세스가 중단되면 다시 실행시키기 위해 감시하는 목적으로도 사용

 

3. 사용법

ps --help all

shell에서 위와 같이 help 명령어를 실행하면 ps 명령어에 대한 자세한 도움말을 볼 수 있습니다.

Usage:
 ps [options]

Basic options:
 -A, -e               all processes
 -a                   all with tty, except session leaders
  a                   all with tty, including other users
 -d                   all except session leaders
 -N, --deselect       negate selection
  r                   only running processes
  T                   all processes on this terminal
  x                   processes without controlling ttys

Selection by list:
 -C <command>         command name
 -G, --Group <GID>    real group id or name
 -g, --group <group>  session or effective group name
 -p, p, --pid <PID>   process id
        --ppid <PID>  parent process id
 -q, q, --quick-pid <PID>
                      process id (quick mode)
 -s, --sid <session>  session id
 -t, t, --tty <tty>   terminal
 -u, U, --user <UID>  effective user id or name
 -U, --User <UID>     real user id or name

  The selection options take as their argument either:
    a comma-separated list e.g. '-u root,nobody' or
    a blank-separated list e.g. '-p 123 4567'

Output formats:
 -F                   extra full
 -f                   full-format, including command lines
  f, --forest         ascii art process tree
 -H                   show process hierarchy
 -j                   jobs format
  j                   BSD job control format
 -l                   long format
  l                   BSD long format
 -M, Z                add security data (for SELinux)
 -O <format>          preloaded with default columns
  O <format>          as -O, with BSD personality
 -o, o, --format <format>
                      user-defined format
  s                   signal format
  u                   user-oriented format
  v                   virtual memory format
  X                   register format
 -y                   do not show flags, show rss vs. addr (used with -l)
     --context        display security context (for SELinux)
     --headers        repeat header lines, one per page
     --no-headers     do not print header at all
     --cols, --columns, --width <num>
                      set screen width
     --rows, --lines <num>
                      set screen height

Show threads:
  H                   as if they were processes
 -L                   possibly with LWP and NLWP columns
 -m, m                after processes
 -T                   possibly with SPID column

Miscellaneous options:
 -c                   show scheduling class with -l option
  c                   show true command name
  e                   show the environment after command
  k,    --sort        specify sort order as: [+|-]key[,[+|-]key[,...]]
  L                   show format specifiers
  n                   display numeric uid and wchan
  S,    --cumulative  include some dead child process data
 -y                   do not show flags, show rss (only with -l)
 -V, V, --version     display version information and exit
 -w, w                unlimited output width

        --help <simple|list|output|threads|misc|all>
                      display help and exit

가장 흔하게 사용되는 옵션은 -e-f 옵션입니다.

-e 옵션은 전체 프로세스를 보겠다는 옵션인데 이 옵션을 사용하지 않으면 현재 로그인된 shell에서 실행 중인 프로세스만 표시되기 때문에 대부분의 경우 -e 옵션을 사용합니다.

-A 로도 동일하게 사용 가능하지만 대문자 A보다는 소문자 e가 사용하기 좀 더 편합니다.

[root@test ~]# ps
  PID TTY          TIME CMD
  756 pts/0    00:00:00 ps
23316 pts/0    00:00:01 bash
28992 pts/0    00:00:00 bash
[root@test ~]# ps -e
  PID TTY          TIME CMD
    1 ?        01:16:34 systemd
    2 ?        00:00:14 kthreadd
    3 ?        00:01:10 ksoftirqd/0
    5 ?        00:00:00 kworker/0:0H
    7 ?        00:00:00 migration/0
    8 ?        00:00:00 rcu_bh

...
...
...
 4982 ?        00:02:04 python
31680 ?        00:01:10 qmgr
32662 ?        00:00:00 pickup

-f 옵션은 프로세스의 상태 정보를 모두(full) 표시하겠다는 옵션입니다.

[root@test ~]# ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
root       773 28992  0 15:51 pts/0    00:00:00 ps -f
root     23316 23302  0  2018 pts/0    00:00:01 /bin/bash
root     28992 28988  0 08:25 pts/0    00:00:00 -bash

프로세스를 실행한 UIDPID, 그리고 부모 프로세스의 ID(PPID)도 표시되며, 프로세스를 실행할 당시의 커맨드라인(command-line)도 표시 되기 때문에 자주 사용됩니다.

같은 이름의 프로세스가 여러 개 실행 중일 때는 커맨드 라인으로 프로세스를 구분해야 하기 때문입니다. (nginx 같은 경우) 여러 개의 옵션을 함께 사용할 때는 - 기호는 한번만 써도 됩니다.

[root@test ~]# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0  2018 ?        01:16:34 /usr/lib/systemd/systemd --system --deserialize 21
root         2     0  0  2018 ?        00:00:14 [kthreadd]
root         3     2  0  2018 ?        00:01:10 [ksoftirqd/0]
root         5     2  0  2018 ?        00:00:00 [kworker/0:0H]
root         7     2  0  2018 ?        00:00:00 [migration/0]

-u 옵션을 사용하면 특정 사용자 계정(UID)으로 실행되고 있는 프로세스만 볼 수 있습니다.

[root@localhost ~]# ps -fu node_manager
UID         PID   PPID  C STIME TTY          TIME CMD
node_ma+ 183483      1  0  8월04 ?      00:09:22 java -jar -Xms2048m -Xmx2048m -XX:PermSize=256m -XX:MaxPermSize=512m node_manager-0.0.1-SNAP
node_ma+ 199085 199067  0  8월03 ?      00:00:00 sshd: node_manager@pts/3
node_ma+ 199086 199085  0  8월03 pts/3  00:00:00 -bash

 

4. 예제

ps -ef 명령어와 함께 파이프를 사용해서 특정 문자열을 통해 검색할 수 있습니다.

[root@test ~]# ps -ef | grep python
root       477     1  0  2018 ?        00:00:47 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
root       819     1  0  2018 ?        03:10:37 /usr/bin/python -Es /usr/sbin/tuned -l -P
root       847 28992  0 16:01 pts/0    00:00:00 grep --color=auto python
root      4982  4956  0  2019 ?        00:02:04 python ./get_pr.py
root     28692     1  0  2019 ?        00:00:00 python app.py

이처럼 python 프로세스가 여러 개 실행 중인걸 확인할 수 있고, 커맨드라인을 통해 어떤 스크립트를 실행하고 있는지 구별이 가능합니다.