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 - 217 results
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?

ps aux | awk '$11!~/\[*\]/ {print $6/1024" Mb --> "$11,$12,$13,$14}' | sort -g
2012-03-23 20:59:33
User: dererk
Functions: awk ps sort
2

Works on most unixes, on OpenBSD replace the "-g" parameter at the sort with a "-n".

ps h --ppid $(cat /var/run/apache2.pid) | awk '{print"-p " $1}' | xargs sudo strace
2012-03-21 01:59:41
Functions: awk cat ps sudo xargs
2

Like the original version except it does not include the parent apache process or the grep process and adds "sudo" so it can be run by user.

adb shell ps | grep my.app.packagename | awk '{print $2}' | xargs -I ? sh -c "adb logcat -v time | grep ?"
adb shell ps | grep <process name> | awk '{print $2}' | xargs adb shell kill
2012-03-03 01:03:39
Functions: awk grep ps xargs
0

This is great when you need to reboot the system-server, or your own daemon that has gone crazy

for i in $(ps x | grep chrome | cut -d"?" -f1 | grep -v chrome); do kill -9 $i ; done
2012-03-01 03:41:39
Functions: cut grep kill ps
0

This one liner is to kill all google chrome tabs. This works similar to $ killall firefox command which is to kill all firefox processes.

kill -9 $(ps -ef | grep [h]ttpd | awk '{print $2}')
PID=httpd ; ps aux | grep $PID | grep -v grep | awk '{print $2}' | xargs kill -9
2012-02-21 23:27:47
User: esaenz
Functions: awk grep kill ps xargs
-4

# define user pid to kill

PID=httpd ;

# kill all pids

ps aux | grep $PID | grep -v grep | awk '{print $2}' | xargs kill -9

for i in $(ps -eo pid,pmem,pcpu| sort -k 3 -r|grep -v PID|head -10|awk '{print $1}');do diff -yw <(pidstat -p $i|grep -v Linux) <(ps -o euser,pri,psr,pmem,stat -p $i|tail);done
2012-02-16 20:54:32
Functions: awk diff grep head ps sort
0

It grabs the PID's top resource users with $(ps -eo pid,pmem,pcpu| sort -k 3 -r|grep -v PID|head -10)

The sort -k is sorting by the third field which would be CPU. Change this to 2 and it will sort accordingly.

The rest of the command is just using diff to display the output of 2 commands side-by-side (-y flag) I chose some good ones for ps.

pidstat comes with the sysstat package(sar, mpstat, iostat, pidstat) so if you don't have it, you should.

I might should take off the timestamp... :|

ps -p $$
2012-02-09 17:27:45
Functions: ps
4

works as well as echo $0, but also prints process id, which pts you're using. echo $SHELL doesn't always get updated when changing shells, so this is a better solution than that. Just one more variation on a theme.

ps -fea | grep PATTERN | awk {'print $2'} | xargs kill -9
ps -ef | grep [j]ava | awk -F ' ' ' { print $1," ",$2,"\t",$(NF-2),"\t",$(NF-1),"\t",$NF } ' | sort -k4
2012-01-05 16:05:48
User: drockney
Functions: awk grep ps sort
Tags: sort awk grep ps
0

Tested in bash on AIX & Linux, used for WAS versions 6.0 & up. Sorts by node name.

Useful when you have vertically-stacked instances of WAS/Portal. Cuts out all the classpath/optional parameter clutter that makes a simple "ps -ef | grep java" so difficult to sort through.

sudo ps aux --sort:rss | awk '{print $2"\t"$11": "$6/1024" MB"}' | column -t | less