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

Shahriar picture Shahriar · Feb 7, 2018 · Viewed 9k times · Source

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.

Answer

nickgryg picture nickgryg · Feb 7, 2018

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