WB Lee
WB Lee

Reputation: 841

How to get all process ids without ps command on Linux

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?

Upvotes: 74

Views: 70035

Answers (4)

dmatej
dmatej

Reputation: 1586

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; 
}

Upvotes: 0

Ivan
Ivan

Reputation: 7253

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; }

Upvotes: 40

spume
spume

Reputation: 2004

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.

Upvotes: 64

glenn jackman
glenn jackman

Reputation: 246744

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]))

Upvotes: 43

Related Questions