How can I get process name of specific PID with ps command in alpine

此生再无相见时 提交于 2020-01-02 04:27:07

问题


In ubuntu based docker/os

$ ps 
PID   USER     TIME   COMMAND
    1 postgres   0:00 postgres
   47 postgres   0:00 postgres: checkpointer process   
   48 postgres   0:00 postgres: writer process   
   49 postgres   0:00 postgres: wal writer process   
   50 postgres   0:00 postgres: autovacuum launcher process   
   51 postgres   0:00 postgres: stats collector process   
   52 postgres   0:00 postgres: bgworker: logical replication launcher

Now If run ps -p 1 -o user=, it will get me PID 1 process USER postgres

$ ps -p 1 -o user=
postgres

This is what I can do in ubuntu based image/os

Now

I am really seeking for a way to do the same for alpine based image. Where I can run ps command to get PID 1 process USER.

I didn't find any docs/hints around.


回答1:


There is very cut version of ps in alpine image by default. It is busybox one:

/ # ps --help
BusyBox v1.27.2 (2017-12-12 10:41:50 GMT) multi-call binary.

Usage: ps [-o COL1,COL2=HEADER]

Show list of processes

    -o COL1,COL2=HEADER Select columns for display

It can only show output with defined columns.

If you want use uncut ps, you need to install it first to alpine image:

/ # apk add --no-cache procps
fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/community/x86_64/APKINDEX.tar.gz
(1/6) Installing libintl (0.19.8.1-r1)
(2/6) Installing ncurses-terminfo-base (6.0_p20171125-r0)
(3/6) Installing ncurses-terminfo (6.0_p20171125-r0)
(4/6) Installing ncurses-libs (6.0_p20171125-r0)
(5/6) Installing libproc (3.3.12-r3)
(6/6) Installing procps (3.3.12-r3)
Executing busybox-1.27.2-r7.trigger
OK: 13 MiB in 17 packages

Now, you can use it you want:

/ # ps -p 1 -o user=
root


来源:https://stackoverflow.com/questions/48655709/how-can-i-get-process-name-of-specific-pid-with-ps-command-in-alpine

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!