How to get all process ids without ps command on Linux
Asked Answered
H

4

62

How to get all process ids (pid) (similar to: $ ps aux) but without using ps.

One example of when this would be used is when developing a dotnet 5 application to run on a docker host. The dotnet runtime image is a very cut-down Linux image, with bash, but without ps. When diagnosing an issue with the application, it's sometimes useful to see what processes are running and if separate processes have been spawned correctly. ps is unavailable on this image. Is there an alternative?

Homestead answered 2/10, 2015 at 18:20 Comment(0)
U
42

On Linux, all running process have "metadata" stored in the /proc filesystem.

All running process ids:

shopt -s extglob # assuming bash
(cd /proc && echo +([0-9]))
Urano answered 2/10, 2015 at 18:45 Comment(8)
what is the meaning of echo +([0-9]) .Thanks!Homestead
When I open the filessystems, there are some other information but how the +([0-9]) get the pid. Thanks! Sincerely!Homestead
That's a bash [extended filename globbing pattern]() that matches one or more digits. In the /proc directory, there is a subdirectory for each process (named with the numeric pid), and a bunch of other thingsUrano
Why was this question closed as unclear? The question seems abundantly clear as evidenced by the on-point answer. Can we get this question reopened?Particle
It was most likely closed because there is no effort made by the OP. Note the close message: "Please clarify your specific problem or add additional details to highlight exactly what you need"Urano
Try for exe in /proc/*/exe; do ls -l $exe; doneSower
Want tp clarify that shopt is not included on the current default dotnet docker imageBlowfish
To also show the command line that started the process, after executing shopt -s extglob on its own line, try: for exe in /proc/+([0-9])/exe; do ls -l $exe; echo "Command line with args:"; tr '\0' '\n' < $(dirname $exe)/cmdline; echo -e '\n'; doneTorpid
N
50

Further to the comment by @FelixJongleur42, the command

ls -l /proc/*/exe

yields a parseable output with additional info such as the process user, start time and command.

Nitriding answered 16/2, 2022 at 12:38 Comment(0)
U
42

On Linux, all running process have "metadata" stored in the /proc filesystem.

All running process ids:

shopt -s extglob # assuming bash
(cd /proc && echo +([0-9]))
Urano answered 2/10, 2015 at 18:45 Comment(8)
what is the meaning of echo +([0-9]) .Thanks!Homestead
When I open the filessystems, there are some other information but how the +([0-9]) get the pid. Thanks! Sincerely!Homestead
That's a bash [extended filename globbing pattern]() that matches one or more digits. In the /proc directory, there is a subdirectory for each process (named with the numeric pid), and a bunch of other thingsUrano
Why was this question closed as unclear? The question seems abundantly clear as evidenced by the on-point answer. Can we get this question reopened?Particle
It was most likely closed because there is no effort made by the OP. Note the close message: "Please clarify your specific problem or add additional details to highlight exactly what you need"Urano
Try for exe in /proc/*/exe; do ls -l $exe; doneSower
Want tp clarify that shopt is not included on the current default dotnet docker imageBlowfish
To also show the command line that started the process, after executing shopt -s extglob on its own line, try: for exe in /proc/+([0-9])/exe; do ls -l $exe; echo "Command line with args:"; tr '\0' '\n' < $(dirname $exe)/cmdline; echo -e '\n'; doneTorpid
U
35

This one-liner will give you the pid and the cmd with args:

for prc in /proc/*/cmdline; { (printf "$prc "; cat -A "$prc") | sed 's/\^@/ /g;s|/proc/||;s|/cmdline||'; echo; }
Undertaking answered 28/10, 2022 at 18:43 Comment(2)
very nice, thank you!Saks
In debian it's for prc in /proc/*/cmdline; do (printf "$prc "; cat -A "$prc") | sed 's/\^@/ /g;s|/proc/||;s|/cmdline||'; echo; doneSquint
S
0

Based on Ivan's example with some filtering:

for prc in /proc/*/cmdline; { 
    (printf "$prc "; cat -A "$prc") | sed 's/\^@/ /g;s|/proc/||;s|/cmdline||' | grep java ; echo -n; 
}
Sammer answered 8/12, 2022 at 11:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.