Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. 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.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

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:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands using ps from sorted by
Terminal - Commands using ps - 206 results
renice -20 -g 2874 (2784 found with ps -Aj)
ps aux | grep [process] | awk '{print $2}' | xargs -I % ls /proc/%/fd | wc -l
ps aux | grep <process> | grep -v grep | awk '{print $2}' | xargs -i -t kill -9 {}
ps auxw | grep sbin/apache | awk '{print"-p " $2}' | xargs strace -f
2013-02-19 19:14:57
User: msealand
Functions: awk grep ps strace xargs
1

This version also attaches to new processes forked by the parent apache process. That way you can trace all current and *future* apache processes.

ps -u user_name_here | grep process_name_here | wc -l
ps -xaw -o state,ppid | grep Z | grep -v PID | awk '{ print $2 }' | xargs kill -9
2013-01-09 04:21:54
User: terrywang
Functions: awk grep kill ps xargs
-4

Did some research and found the previous command wrong, we don't kill a zombie but its parent. Just made some modifcation to khashmeshab's command.

ps axu | grep [a]pache2
2012-12-15 19:37:19
User: EBAH
Functions: grep ps
12

Trick to avoid the form:

grep process | grep - v grep

while true; do date; ps auxf | awk '{if($8=="D") print $0;}'; sleep 1; done
ps -ef | awk -v OFS="\n" '{ for (i=8;i<=NF;i++) line = (line ? line FS : "") $i; print NR ":", $1, $2, $7, line, ""; line = "" }'
ps -eo size,pid,user,command --sort -size |awk '{hr[1024**2]="GB";hr[1024]="MB";for (x=1024**3; x>=1024; x/=1024){if ($1>=x){printf ("%-6.2f %s ", $1/x, hr[x]);break}}}{printf ("%-6s %-10s ", $2, $3)}{for (x=4;x<=NF;x++){printf ("%s ",$x)} print ("\n")}'
ps aux --sort -rss | head
for i in $(ps -eo pid|grep -v PID);do echo ""; echo -n "==$i== ";awk '/^read|^write/{ORS=" "; print}' /proc/$i/io 2>/dev/null; echo -n " ==$i=="; done|sort -nrk5|awk '{printf "%s\n%s %s\n%s %s\n%s\n\n",$1,$2,$3,$4,$5,$6}'
export proc=chrome && ps aux | grep $proc | grep -v grep |awk '{print $2}'
ps axo %mem,pid,euser,cmd | sort -nr | head -n 10
ps wwwwuax|awk '/command/ { printf("kill -9 %s\n",$2) }'|/bin/sh
2012-08-14 21:44:38
User: jetdillo
Functions: awk ps
0

Okay, commands like this are a bit of a personal peeve. awk(1) operates on a /pattern/ {action} paradigm and yet I see people leave out the /pattern/ portion of an awk command all the time, opting to use grep or sed instead. You'll save yourself some typing and time if you include the /pattern/ with your {action}.

ps -fu $USER | awk {'print $2'} | xargs kill [-9]
ps auxww | grep application | grep processtobekilled | gawk '{print $2}' | grep -v grep | xargs kill -9
2012-07-03 20:37:56
User: j0sh10
Functions: gawk grep kill ps xargs
0

You can also use gawk:

ps auxww | gawk '/application/' | gawk '/processtobekilled/' | gawk '{print $2}' | grep -v grep | xargs kill -9

ps aux | awk '{if ($8 ~ "D") print $0}'
2012-05-25 16:53:57
User: SEJeff
Functions: awk ps
0

Lots of fun to run on nfs clients when the server or network connection is having issues

kill -9 -$(ps x -o "%c %r" | awk '/svscan/{print $2}')
2012-05-25 16:39:02
User: SEJeff
Functions: awk kill ps
0

Daemontools[1] won't always properly reap it's children. Sometimes when you need to kill the main svscan process, you want to also clean up all of it's children. The way to do that is to send a signal to the entire process group. It is a bit tricky

[1] http://cr.yp.to/daemontools.html

ps -e h -o pid --sort -pcpu | head -10 | vzpid -
2012-05-24 14:16:40
User: mrkmg
Functions: head ps
Tags: openvz
0

This command will list the PID, VEID, and Name of the 10 highest cpu using processes on a openvz host. You must have vzpid installed.

slow () { [ -n $1 ] && while ps -p $1 >/dev/null ; do kill -STOP $1; sleep 1; kill -CONT $1; sleep 1; done & }
2012-05-16 12:13:30
User: igorfu
Functions: kill ps sleep
Tags: io
1

Some IO intensive process make the system unresponsive. This function periodically starts/stops a process, which hopefully releases some resources for other activities.

This function is useful when ionice is not available

ps afx | grep defunct -B 1 | grep -Eo "[0-9]{3,}" | xargs kill -9
2012-04-27 16:16:34
User: pholz
Functions: grep kill ps xargs
0

defunct processes (zombies) usually have to be killed by killing their parent processes. this command retrieves such zombies and their immediate parents and kills all of the matching processes.

ps afx|grep [a]pache
2012-04-16 03:50:32
User: caruccio
Functions: grep ps
Tags: ps grep
1

When you 'ps|grep' for a given process, it turns out that grep itself appears as a valid line since it contains the RE/name you are looking for. To avoid grep from showing itself, simply insert some wildcard into process' name.

_p(){ ps ax |grep $1 |sed '/grep.'"$1"'/d' |while read a;do printf ${a%% *}' ';printf "${a#* }" >&2;printf '\n';done;}
2012-04-01 19:46:19
User: argv
Functions: grep printf ps read sed
0

proc lister

usage: p

proc killer

usage: p patt [signal]

uses only ps, grep, sed, printf and kill

no need for pgrep/pkill (not part of early UNIX)

_p(){

ps ax \

|grep $1 \

|sed '

/grep.'"$1"'/d' \

|while read a;do

printf ${a%% *}' ';

printf "${a#* }" >&2;

printf '\n';

done;

}

p(){

case $# in

0)

ps ax |grep .|less -iE;

;;

1)

_p $1;

;;

[23])

_p $1 2>/dev/null \

|sed '/'"$2"'/!d;

s,.*,kill -'"${3-15}"' &,'|sh -v

;;

esac;

}

alas, can't get this under 255 chars.

flatcap?

_p(){ ps ax |grep $1 |sed '/grep.'"$1"'/d' |while read a;do printf ${a%% *}' ';printf "${a#* }" >&2;printf '\n';done;}
2012-04-01 19:45:17
User: argv
Functions: grep printf ps read sed
0

proc lister

usage: p

proc killer

usage: p patt [signal]

uses only ps, grep, sed, printf and kill

no need for pgrep/pkill (not part of early UNIX)

_p(){

ps ax \

|grep $1 \

|sed '

/grep.'"$1"'/d' \

|while read a;do

printf ${a%% *}' ';

printf "${a#* }" >&2;

printf '\n';

done;

}

p(){

case $# in

0)

ps ax |grep .|less -iE;

;;

1)

_p $1;

;;

[23])

_p $1 2>/dev/null \

|sed '/'"$2"'/!d;

s,.*,kill -'"${3-15}"' &,'|sh -v

;;

esac;

}

alas, can't get this under 255 chars.

flatcap?