I want to get information about all processes in the system except the ps
process itself.
That is, something like:
ps --no-headers --pid 1111 -N
where 1111
is the process id of the currently running ps
command.
I tried something like
ps --no-headers --ppid $$ -N
However, this does not work when ps
is running in a "subprocess" like:
IFS=' ' read pctd proc <<<$(/bin/ps --no-headers --ppid $$ -No pcpu,pid,comm --sort=+pcpu | tail -n 1)
This last command tries to get the CPU usage percentage and process id plus name of the process using the most CPU "recently".
IFS=' ' read pctd proc <<<$(/bin/ps --no-headers -eo pcpu,pid,comm --sort=+pcpu | tail -1)
was working OK until recently. However, after update to Ubuntu 23.04 (or kernel 6.x) ps
started sometimes listing itself as using 100% CPU.
For the time being, the following seems as a workaround (but it is not safe):
IFS=' ' read pctd proc <<<$(/bin/ps --no-headers -eo pcpu,pid,comm --sort=+pcpu | grep -v \ ps | tail -1)