Skip to content
Advertisement

how to redirect ps command in linux to look into other folder than /proc

I am using docker containers and I have mounted host /proc to container /host/proc. Now I want ps command inside docker container to look for processes into /host/proc instead of /proc. or how can I right similar utility to ps

Advertisement

Answer

the /proc path is hardcoded in the source tree of the /bin/ps binary file. Thus, you need to recompile /bin/ps

Follow these steps to recompile /bin/ps inside a container that mounts the host /proc and use this new ps to display the process list of the docker host from the container:

In this example, I do not use /host/proc but /prod, to avoid modifying the path length to the mount point of the procfs filesystem (increasing the path length could trigger runtime errors in some situations). I also use a container based on OpenSUSE Leap 42.1, since you have not described the base image you are using.

1- on the docker host, first download the /bin/ps sources:

JavaScript

2- replace any occurence of "/proc by "/prod in **/*.c

3- run your docker container, mounting procfs on /prod in the container

JavaScript

4- inside the docker container, recompile /bin/ps

JavaScript

Now, you can use /root/git/procps/ps/pscommand as a replacement for /bin/ps, in order to use /prod instead of /proc. With pscommand, you will get a process list from the host, not from the container:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement