Commands using ps (300)

  • ps returns all running processes which are then sorted by the 4th field in numerical order and the top 10 are sent to STDOUT. Show Sample Output


    106
    ps aux | sort -nk +4 | tail
    root · 2009-01-23 17:12:33 294
  • As an alternative to using an additional grep -v grep you can use a simple regular expression in the search pattern (first letter is something out of the single letter list ;-)) to drop the grep command itself. Show Sample Output


    72
    ps aux | grep [p]rocess-name
    olorin · 2009-08-13 05:44:45 27
  • If you want a visual representation of the parent/child relationships between processes, this is one easy way to do it. It's useful in debugging collections of shell scripts, because it provides something like a call traceback. When a shell script breaks, just remember "awwfux".


    43
    ps awwfux | less -S
    ToyKeeper · 2009-07-04 09:39:28 15
  • awk is evil! Show Sample Output


    22
    ps hax -o user | sort | uniq -c
    buzzy · 2010-04-29 10:43:03 13
  • ps and grep is a dangerous combination -- grep tries to match everything on each line (thus the all too common: grep -v grep hack). ps -C doesn't use grep, it uses the process table for an exact match. Thus, you'll get an accurate list with: ps -fC sh rather finding every process with sh somewhere on the line. Show Sample Output


    14
    ps -fC PROCESSNAME
    pooderbill · 2015-04-20 13:09:44 17
  • you can also pipe it to "tail" command to show 10 most memory using processes. Show Sample Output


    13
    ps aux --sort=%mem,%cpu
    mrwill · 2009-10-10 22:48:51 7
  • Trick to avoid the form: grep process | grep - v grep Show Sample Output


    13
    ps axu | grep [a]pache2
    EBAH · 2012-12-15 19:37:19 33
  • The trick here is to use the brackets [ ] around any one of the characters of the grep string. This uses the fact that [?] is a character class of one letter and will be removed when parsed by the shell. This is useful when you want to parse the output of grep or use the return value in an if-statement without having its own process causing it to erroneously return TRUE. Show Sample Output


    12
    ps aux | grep "[s]ome_text"
    SiegeX · 2009-02-17 02:10:50 12
  • That is useful to discover the start time of process older than 1 day. You can also run: ls -ld /proc/PID That's returning the creation date of the proc files from the process. Some users reported that this way might show you a wrong date since any other process like cron, for example, could change this date. Show Sample Output


    12
    ps -eo pid,lstart,cmd
    kruspemsv · 2013-06-17 12:52:53 15
  • Surround the first letter of what you are grepping with square brackets and you won't have to spawn a second instance of grep -v. You could also use an alias like this (albeit with sed): alias psgrep='ps aux | grep $(echo $1 | sed "s/^\(.\)/[\1]/g")'


    10
    ps aux | grep [h]ttpd
    abcde · 2009-02-10 02:59:20 16
  • This command is useful when you want to know what process is responsible for a certain GUI application and what command you need to issue to launch it in terminal. Show Sample Output


    9
    xprop | awk '/PID/ {print $3}' | xargs ps h -o pid,cmd
    jackhab · 2009-02-16 07:55:19 83

  • 9
    ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu | sed "/^ 0.0 /d"
    fbparis · 2009-03-07 16:47:10 21
  • I don't truly enjoy many commands more than this one, which I alias to be ps1.. Cool to be able to see the heirarchy and makes it clearer what need to be killed, and whats really going on. Show Sample Output


    8
    command ps -Hacl -F S -A f
    AskApache · 2009-08-19 07:08:19 10
  • The original version gives an error, here is the correct output


    7
    ps -eo user,pcpu,pmem | tail -n +2 | awk '{num[$1]++; cpu[$1] += $2; mem[$1] += $3} END{printf("NPROC\tUSER\tCPU\tMEM\n"); for (user in cpu) printf("%d\t%s\t%.2f\t%.2f\n",num[user], user, cpu[user], mem[user]) }'
    georgz · 2009-10-29 12:49:01 11
  • This one-liner will use strace to attach to all of the currently running apache processes output and piped from the initial "ps auxw" command into some awk. Show Sample Output


    7
    ps auxw | grep sbin/apache | awk '{print"-p " $2}' | xargs strace
    px · 2011-03-14 21:45:22 34

  • 6
    ps -o %mem= -C firefox-bin | sed -s 's/\..*/%/'
    susannakaukinen · 2009-03-06 21:09:26 10
  • preferred way to query ps for a specific process name (not supported with all flavors of ps, but will work on just about any linux afaik) Show Sample Output


    6
    ps -C command
    recursiverse · 2009-08-14 15:30:42 6
  • enumerates the number of processes for each user. ps BSD format is used here , for standard Unix format use : ps -eLf |awk '{$1} {++P[$1]} END {for(a in P) if (a !="UID") print a,P[a]}' Show Sample Output


    6
    ps aux |awk '{$1} {++P[$1]} END {for(a in P) if (a !="USER") print a,P[a]}'
    benyounes · 2010-04-28 15:25:18 3
  • faster ;) but your idea is really cool


    6
    ps -ef | grep c\\ommand
    ioggstream · 2011-01-04 11:43:14 4
  • # Delete all containers docker rm $(docker ps -a -q) # Delete all images docker rmi $(docker images -q)


    6
    sudo docker rm $(docker ps -a -q); sudo docker rmi $(docker images -q)
    lpalgarvio · 2015-05-20 12:34:40 34
  • Shows a tree view of parent to child processes in the output of ps (linux). Similar output can be achieved with pstree (also linux) or ptree (Solaris). Show Sample Output


    5
    ps auxf
    systemj · 2009-02-05 18:07:16 18
  • works well in crontab.


    5
    ps -C program_name || { program_name & }
    grokskookum · 2009-09-24 03:35:39 3
  • This command loops over all of the processes in a system and creates an associative array in awk with the process name as the key and the sum of the RSS as the value. The associative array has the effect of summing a parent process and all of it's children. It then prints the top ten processes sorted by size. Show Sample Output


    5
    ps axo rss,comm,pid | awk '{ proc_list[$2]++; proc_list[$2 "," 1] += $1; } END { for (proc in proc_list) { printf("%d\t%s\n", proc_list[proc "," 1],proc); }}' | sort -n | tail -n 10
    d34dh0r53 · 2010-03-03 16:41:05 5
  • This will save your open windows to a file (~/.windows). To start those applications: cat ~/.windows | while read line; do $line &; done Should work on any EWMH/NetWM compatible X Window Manager. If you use DWM or another Window Manager not using EWMH or NetWM try this: xwininfo -root -children | grep '^ ' | grep -v children | grep -v '<unknown>' | sed -n 's/^ *\(0x[0-9a-f]*\) .*/\1/p' | uniq | while read line; do xprop -id $line _NET_WM_PID | sed -n 's/.* = \([0-9]*\)$/\1/p'; done | uniq -u | grep -v '^$' | while read line; do ps -o cmd= $line; done > ~/.windows Show Sample Output


    5
    wmctrl -l -p | while read line; do ps -o cmd= "$(echo "$line" | awk '$0=$3')"; done > ~/.windows
    matthewbauer · 2010-07-04 22:11:24 5
  • STARTED Mon Oct 18 04:02:01 2010 Show Sample Output


    5
    ps -o lstart <pid>
    nottings · 2010-10-18 17:09:02 3
  •  1 2 3 >  Last ›

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


Check These Out

back up your commandlinefu contributed commands
Use `zless` to read the content of your *rss.gz file: $ zless commandlinefu-contribs-backup-2009-08-10-07.40.39.rss.gz

Convert seconds to [DD:][HH:]MM:SS
Converts any number of seconds into days, hours, minutes and seconds. sec2dhms() { declare -i SS="$1" D=$(( SS / 86400 )) H=$(( SS % 86400 / 3600 )) M=$(( SS % 3600 / 60 )) S=$(( SS % 60 )) [ "$D" -gt 0 ] && echo -n "${D}:" [ "$H" -gt 0 ] && printf "%02g:" "$H" printf "%02g:%02g\n" "$M" "$S" }

Let's make screen and ssh-agent friends
When you start screen as `ssh-agent screen`, agent will die after detatch. If you don't want to take care about files when stored agent's pid/socket/etc, you have to use this command.

Which processes are listening on a specific port (e.g. port 80)
swap out "80" for your port of interest. Can use port number or named ports e.g. "http"

Find the package that installed a command

Convert seconds to [DD:][HH:]MM:SS
Converts any number of seconds into days, hours, minutes and seconds. sec2dhms() { declare -i SS="$1" D=$(( SS / 86400 )) H=$(( SS % 86400 / 3600 )) M=$(( SS % 3600 / 60 )) S=$(( SS % 60 )) [ "$D" -gt 0 ] && echo -n "${D}:" [ "$H" -gt 0 ] && printf "%02g:" "$H" printf "%02g:%02g\n" "$M" "$S" }

Get the size of all the directories in current directory (Sorted Human Readable)
This allows the output to be sorted from largest to smallest in human readable format.

Remove spaces from filenames - through a whole directory tree.
An example of zsh glob qualifiers.

Recursively grep for string and format output for vi(m)
This is a big time saver for me. I often grep source code and need to edit the findings. A single highlight of the mouse and middle mouse click (in gnome terminal) and I'm editing the exact line I just found. The color highlighting helps interpret the data.

Unite pdf files
pdfunite is a part of the poppler-utils. poppler-utils package is only 150KB. The alternative - pdftk package is 14MB! Install poppler-utils if you need simple pdf operation commands like unite, separate, info, text/html conversions


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: