View All Processess Cmdlines and Environments

cd /proc&&ps a -opid=|xargs -I+ sh -c '[[ $PPID -ne + ]]&&echo -e "\n[+]"&&tr -s "\000" " "<+/cmdline&&echo&&tr -s "\000\033" "\nE"<+/environ|sort'
Grabs the cmdline used to execute the process, and the environment that the process is being run under. This is much different than the 'env' command, which only lists the environment for the shell. This is very useful (to me at least) to debug various processes on my server. For example, this lets me see the environment that my apache, mysqld, bind, and other server processes have. Here's a function I use: aa_ps_all () { ( cd /proc && command ps -A -opid= | xargs -I'{}' sh -c 'test $PPID -ne {}&&test -r {}/cmdline&&echo -e "\n[{}]"&&tr -s "\000" " "<{}/cmdline&&echo&&tr -s "\000\033" "\nE"<{}/environ|sort&&cat {}/limits' ); } From my .bash_profile at http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html
Sample Output
[32447]
/usr/bin/screen -A -D -r -a -S sh-srot -T xterm-256color -O -l 
AA_P=export PVE="\033[m\033[38;5;2m"$((`sed '2!d;s/[^0-9]*//g' /proc/meminfo`/1024))"\033[38;5;22m/"$((`sed '1!d;s/[^0-9]*//g' /proc/meminfo`/1024))MB"\t\033[m\033[38;5;55m$(< /proc/loadavg)\033[m";echo -en ""
BKDIR=/home/askapache/.bk
BROWSER=/usr/bin/lynx
CR=E(BE[mE[!pE[?3;4lE[4lE>
CVS_RSH=ssh
EDITOR=/usr/bin/vim
GROUPNAME=srot
G_BROKEN_FILENAMES=1
HISTCONTROL=ignoreboth
HISTFILE=/home/askapache/.bk/.history/.bash_history
HISTFILESIZE=5000
HISTIGNORE=cls:reset:clear:ls:ll:lll:l:pwd:env:printenv:lsof:updatedb:h2:h1:h3:dir:cd ..:date:exit:aa_screen
HISTSIZE=1000
HOME=/home/askapache
HOSTNAME=www.askapache.com
IFS= 
INPUTRC=/etc/inputrc
LANG=C
LESSCHARSET=latin1
LESSOPEN=|/usr/bin/lesspipe.sh %s
LOGNAME=askapache
N6=/dev/null
PAGER=less
PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin:/usr/kerberos/bin:/usr/libexec
PF=%.5m %10M %#15s %#9u:%-9g %#5U:%-5G  [%AD | %CD | %TD]  [%Y] %p\n
PKG_CONFIG_PATH=/usr/lib/pkgconfig:/usr/local/lib/pkgconfig
PROMPT_COMMAND=history -a;(($SECONDS % 10==0 ))&&eval "$AA_P";echo -en "$PVE"
PS1=\[\e[m\n\e[1;30m\][$$:$PPID \j:\!\[\e[1;30m\]]\[\e[0;36m\] \T \d \[\e[1;30m\][\[\e[1;34m\]\u@\H\[\e[1;30m\]:\[\e[0;37m\]${SSH_TTY/\/dev\/} \[\e[0;32m\]+${SHLVL}\[\e[1;30m\]] \[\e[1;37m\]\w\[\e[0;37m\]\n\$ 
PVE=\033[m\033[38;5;2m1849\033[38;5;22m/2048MB\t\033[m\033[38;5;55m1.09 1.25 0.90 1/66 15024\033[m
PWD=/home/askapache
R=E[?12lE[?25hE[27mE[24mE(BE[m
SHELL=/bin/bash
SHLVL=1
TERM=xterm-256color
TMPDIR=/tmp
TZ=America/Indiana/Indianapolis
USER=askapache
_=/usr/bin/screen

These Might Interest You

What Others Think

Literally one of my all-time best commands, why no votes? Any ideas?
AskApache · 398 weeks and 3 days ago
@AskApache: sh: [[: not found it works fine if you'll change sh -c to bash -c, though.
depesz · 398 weeks ago
@depesz, thanks, its fixed at: http://www.commandlinefu.com/commands/view/10047/show-cmdlines-for-all-processes sh -O extglob -Bc 'sed "s/\x00/ /g" /proc/!([^0-9]*|$$)/cmdline'
AskApache · 335 weeks and 1 day ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: