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 kill from sorted by
Terminal - Commands using kill - 92 results
kill -9 `ps aux | grep "search_criteria" | awk '{if ($2 != pid) print $2}'`
2010-09-16 08:52:59
User: ozgurkuru
Functions: awk grep kill
-6

search_criteria = what do you want to kill

pid = pid of you dont kill

kill HUP `pidof '/usr/bin/X'`
ps auxwww | grep outofcontrolprocess | awk '{print $2}' | xargs kill -9
kill -9 `pgrep $PROCESS_NAME`
2010-06-18 20:43:24
User: juanse006
Functions: kill
-5

The pgrep retrieves the PID, then the KILL receive it, and kill it...

It works also if the application has more than one instance....

$COMMAND 2>&1 >/dev/null & WPID=$!; sleep $TIMEOUT && kill $! & KPID=$!; wait $WPID
2010-05-26 11:12:26
User: keymon
Functions: kill sleep wait
3

I like much more the perl solution, but without using perl. It launches a backgroup process that will kill the command if it lasts too much.

A bigger function:

check_with_timeout() {

[ "$DEBUG" ] && set -x

COMMAND=$1

TIMEOUT=$2

RET=0

# Launch command in backgroup

[ ! "$DEBUG" ] && exec 6>&2 # Link file descriptor #6 with stderr.

[ ! "$DEBUG" ] && exec 2> /dev/null # Send stderr to null (avoid the Terminated messages)

$COMMAND 2>&1 >/dev/null &

COMMAND_PID=$!

[ "$DEBUG" ] && echo "Background command pid $COMMAND_PID, parent pid $$"

# Timer that will kill the command if timesout

sleep $TIMEOUT && ps -p $COMMAND_PID -o pid,ppid |grep $$ | awk '{print $1}' | xargs kill &

KILLER_PID=$!

[ "$DEBUG" ] && echo "Killer command pid $KILLER_PID, parent pid $$"

wait $COMMAND_PID

RET=$?

# Kill the killer timer

[ "$DEBUG" ] && ps -e -o pid,ppid |grep $KILLER_PID | awk '{print $1}' | xargs echo "Killing processes: "

ps -e -o pid,ppid |grep -v PID | grep $KILLER_PID | awk '{print $1}' | xargs kill

wait

sleep 1

[ ! "$DEBUG" ] && exec 2>&6 6>&- # Restore stderr and close file descriptor #6.

return $RET

}

pgrep rouge-process | xargs sudo kill -9
2010-05-09 22:30:05
User: mheadd
Functions: kill sudo xargs
Tags: xargs pgrep
-3

Find and kill multiple instances of a process with one simple command.

kill -9 `pgrep firefox`
2010-05-02 23:05:25
User: dustindorroh
Functions: kill
-11
pgrep firefox

- this returns the Process ID for the signal firefox

kill -9 pid

- this kills the signal that had the given pid

cmd1 `cmd1` or cmd1 $(cmd1)

- here is the syntax for cmd2 to be executed first. Then the call to cmd2 is substituted with the output of cmd2, and cmd1 is executed.

tail -n0 -f access.log>/tmp/tmp.log & sleep 10; kill $! ; wc -l /tmp/tmp.log
2010-04-29 21:23:46
User: dooblem
Functions: kill sleep tail wc
Tags: tail kill wc sleep
1

Another way of counting the line output of tail over 10s not requiring pv.

Cut to have the average per second rate :

tail -n0 -f access.log>/tmp/tmp.log & sleep 10; kill $! ; wc -l /tmp/tmp.log | cut -c-2

You can also enclose it in a loop and send stderr to /dev/null :

while true; do tail -n0 -f access.log>/tmp/tmp.log & sleep 2; kill $! ; wc -l /tmp/tmp.log | cut -c-2; done 2>/dev/null

very_long_command& sleep 10; kill $!
2010-04-29 20:43:13
User: dooblem
Functions: kill sleep
5

or "Execute a command with a timeout"

Run a command in background, sleep 10 seconds, kill it.

! is the process id of the most recently executed background command.

You can test it with:

find /& sleep10; kill $!

kill -3 PID
2010-04-28 08:22:42
User: mrbyte
Functions: kill
Tags: kill java
0

Useful command to get information about running java process and treads, to see log look into the default log for your java application

export var1=`ps -A | grep '[u]nique' | cut -d '?' -f 1`; echo${var1/ /}; kill -9 $var1
2010-03-22 21:06:58
User: headyadmin
Functions: cut export grep kill
Tags: Os X
-5

This will kill a specific process you don't know the PID of, when pidof and pgrep are not available, for example on OS X. var1 is created so that the whitespace can be trimmed before passing off to kill.

dd if=fromfile of=tofile & DDPID=$! ; sleep 1 ; while kill -USR1 $DDPID ; do sleep 5; done
2010-01-12 15:01:44
User: deltaray
Functions: dd kill sleep
Tags: dd kill while sleep
4

This is a more accurate way to watch the progress of a dd process. The $DDPID=$! is needed so that you don't get the PID of the sleep. The sleep 1 is needed because in my testing at least, if you run kill -USR1 against dd too quickly, it will kill it off instead of display the status. So you need to wait a second, probably so that it can configure itself to trap the USR1 signal.

stop () { ps -ec | grep $@ | kill -SIGSTOP `awk '{print $1}'`; }
2009-12-27 19:40:09
User: iridium172
Functions: grep kill ps
1

Add that and "cont () { ps -ec | grep $@ | kill -SIGCONT `awk '{print $1}'`; }" (without the quotes) to you bash profile and then use it to pause and resume processes safely

SOURCE: dd if=/dev/sda bs=16065b | netcat ip-target 1234 TARGET: netcat -l -p 1234 | dd of=/dev/mapper/laptop bs=16065b STATS on target: watch -n60 -- kill -USR1 $(pgrep dd)
2009-12-16 10:51:06
User: bw
Functions: dd kill watch
5

I wanted to create a copy of my whole laptop disk on an lvm disk of the same size.

First I created the logical volume: lvcreate -L120G -nlaptop mylvms

SOURCE: dd if=/dev/sda bs=16065b | netcat ip-target 1234

TARGET: nc -l -p 1234 | dd of=/dev/mapper/mylvms-laptop bs=16065b

to follow its process you issue the following command in a different terminal

STATS: on target in a different terminal: watch -n60 -- kill -USR1 $(pgrep dd)

(see http://www.commandlinefu.com/commands/view/4356/output-stats-from-a-running-dd-command-to-see-its-progress)

ps ax| awk '/[h]ttpd/{print $1}'| xargs kill -9
for i in `mysqladmin -h x.x.x.x --user=root -pXXXX processlist | grep <<username>>| grep <<Locked>>| awk {'print $2'}` do mysqladmin -h x.x.x.x --user=root -pXXX kill $i; done;
ps aux| grep -v grep| grep httpd| awk {'print $2'}| xargs kill -9
export HISTFILE=/dev/null && kill -9 $$
rm ~/.bash_history && kill -9 $$
2009-10-08 12:25:47
User: Velenux
Functions: kill rm
-5

Best way I know to get rid of .bash_history and don't allow bash to save the current one on exit

Edit: added ~/ before .bash_history, just in case... ;)

find /proc -user myuser -maxdepth 1 -type d -mtime +7 -exec basename {} \; | xargs kill -9
kill -9 <replace this with pid you want to kill>
gate() { mkfifo /tmp/sock1 /tmp/sock2 &> /dev/null && nc -p $1 -l < /tmp/sock1 | tee /tmp/sock2 & PID=$! && nc $2 $3 < /tmp/sock2 | tee /tmp/sock1; kill -KILL $PID; rm -f /tmp/sock1 /tmp/sock2 ; }
2009-09-25 08:10:23
User: true
Functions: kill mkfifo rm tee
1

USAGE: gate listening_port host port

Creates listening socket and connects to remote device at host:port. It uses pipes for connection between two sockets. Traffic which goes through pipes is wrote to stdout. I use it for debug network scripts.

tokill=`ps -fea|grep process|awk '{ printf $2" "}'`; kill -9 $tokill;
kill_daemon() { echo "Daemon?"; read dm; kill -15 $(netstat -atulpe | grep $dm | cut -d '/' -f1 | awk '{print $9}') }; alias kd='kill_daemon
2009-05-26 20:39:56
User: P17
-5

Just find out the daemon with $ netstat -atulpe. Then type in his name and he gets the SIGTERM.

kill -0 [pid]
2009-05-19 11:37:20
User: sharfah
Functions: kill
Tags: kill
5

Send signal 0 to the process. The return status ($?) can be used to determine if the process is running. 0 if it is, non-zero otherwise.