Commands using kill (107)

  • this exits bash without saving the history. unlike explicitly disabling the history in some way, this works anywhere, and it works if you decide *after* issuing the command you don't want logged, that you don't want it logged ... $$ ( or ${$} ) is the pid of the current bash instance this also works perfectly in shells that don't have $$ if you do something like kill -9 `readlink /proc/self`

    kill -9 $$
    stu · 2009-03-27 23:13:53 13

  • 12
    find /proc -user myuser -maxdepth 1 -type d -mtime +7 -exec basename {} \; | xargs kill -9
    sharfah · 2009-10-05 14:49:51 2

  • 12
    kill -9 -1
    cogsmos · 2010-11-08 03:28:43 2
  • It identifies the parents of the Zombie processes and kill them. So the new parent of orphan Zombies will be the Init process and he is already waiting for reaping them. Be careful! It may also kill your useful processes just because they are not taking care and waiting for their children (bad parents!). Show Sample Output

    kill -9 `ps -xaw -o state -o ppid | grep Z | grep -v PID | awk '{print $2}'`
    khashmeshab · 2010-10-27 07:29:14 7
  • This command seems to achieve the similar/same goal.

    kill -l
    b_t · 2010-11-17 23:48:51 0
  • 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

    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)
    bw · 2009-12-16 10:51:06 1
  • 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 $!

    very_long_command& sleep 10; kill $!
    dooblem · 2010-04-29 20:43:13 1

  • 5
    kill -9 `ps -u <username> -o "pid="`
    skye · 2009-03-23 17:51:37 4
  • 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.

    kill -0 [pid]
    sharfah · 2009-05-19 11:37:20 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. Show Sample Output

    dd if=fromfile of=tofile & DDPID=$! ; sleep 1 ; while kill -USR1 $DDPID ; do sleep 5; done
    deltaray · 2010-01-12 15:01:44 1
  • 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 }

    $COMMAND 2>&1 >/dev/null & WPID=$!; sleep $TIMEOUT && kill $! & KPID=$!; wait $WPID
    keymon · 2010-05-26 11:12:26 0
  • Somtime one wants to kill process not by name of executable, but by a parameter name. In such cases killall is not suitable method.

    kill -9 `ps ax | egrep [f]elix.jar | egrep -o -e '^ *[0-9]+'`
    yababay · 2010-09-30 16:45:47 0
  • This is a 'killall' command equivalent where it is not available. Prior to executing it, set the environment variable USERNAME to the username, whose processes you want to kill or replace the username with the $USERNAME on the command above. Side effect: If any processes from other users, are running with a parameter of $USERNAME, they will be killed as well (assuming you are running this as root user) [-9] in square brackets at the end of the command is optional and strongly suggested to be your last resort. I do not like to use it as the killed process leaves a lot of mess behind.

    ps -ef | grep $USERNAME | awk {'print $2'} | xargs kill [-9]
    TheNomad · 2009-02-17 20:35:35 2
  • Logs all users out except root. I changed the grep to use a regexp in case a user's username contained the word root.

    who -u | grep -vE "^root " | kill `awk '{print $7}'`
    ProfessorTux · 2010-11-05 17:43:41 8
  • Adjust "sleep X" to your needs. *NOTE: First sleep is required because bash doesn't have a "post-test" syntax (do XXX while). Show Sample Output

    dd if=/path/to/inputfile of=/path/to/outputfile & pid=$! && sleep X && while kill -USR1 $pid; do sleep X; done
    cyrusza · 2010-12-02 15:07:18 3

  • 2
    for i in `ps aux | grep ssh | grep -v grep | awk {'print $2'}` ; do kill $i; done
    jcwaters · 2009-02-21 02:07:22 6

  • 2
    kill HUP `pidof '/usr/bin/X'`
    aceiro · 2010-07-16 03:55:13 1
  • This shell function takes a single argument, which is used as the base name of the .wav, .timing and .session files created. To create a screencast: screencast test type and talk ... then type 'exit' or to exit the screencast. test.wav will contain the audio from your screencast. test.session will contain text and control characters needed to paint the screen test.timing will contain timing information needed to synch individual keystrokes in test.session with the audio. to play back: aplay test.wav & scriptreplay test.{timing,session} NOTE: because the shell function uses the variable "$!", and bash likes to expand '!' during history expansion, you will need to turn off bash's history before you enter the shell function. This can be achieved using the command set +H

    screencast() { arecord -R 1000 -f cd -t wav $1.wav & RECPID=$!; echo "Starting screencast in new shell. Exit subshell to quit."; script -t 2> $1.timing -a $1.session; kill $RECPID; }
    bartonski · 2011-01-20 14:35:47 0
  • Only slightly different than previous commands. The benefit is that your "watch" should die when the dd command has completed. (Of course this would depend on /proc being available)

    dd if=/dev/urandom of=file.img bs=4KB& pid=$!; while [[ -d /proc/$pid ]]; do kill -USR1 $pid && sleep 1 && clear; done
    unixmonkey14934 · 2011-06-24 21:49:10 2
  • alternatively, run the spinner for 5 seconds: timeout 5 bash -c 'spinner=( Ooooo oOooo ooOoo oooOo ooooO oooOo ooOoo oOooo); while true; do for i in ${spinner[@]}; do for j in seq 0 ${#i}; do echo -en "\b\b"; done; echo -ne "${i}"; sleep 0.2; done; done' Show Sample Output

    while kill -0 0; do timeout 5 bash -c 'spinner=( Ooooo oOooo ooOoo oooOo ooooO oooOo ooOoo oOooo); while true; do for i in ${spinner[@]}; do for _ in seq 0 ${#i}; do echo -en "\b\b"; done; echo -ne "${i}"; sleep 0.2; done; done'; done
    anapsix · 2015-05-07 19:13:08 3
  • Kill all processes with foo in them. Similar to pkill but more complete and also works when there is no pkill command. Works on almost every Linux/Unix platform I have tried.

    ps -ef | grep [f]oo | awk '{print $2}' | xargs kill -9
    eredicatorx · 2009-02-05 13:43:01 9
  • explanation: grep -- displays process ids -v -- negates the matching, displays all but what is specified in the other options -u -- specifies the user to display, or in this case negate The process loops through all PIDs that are found by pgrep, then orders a forced kill to the processes in numerical order, effectively killing the parent processes first including the shells in use which will force the users to logout. Tested on Slackware Linux 12.2 and Slackware-current

    for i in $(pgrep -v -u root);do kill -9 $i;done
    lostnhell · 2009-03-24 02:54:52 2
  • 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.

    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 ; }
    true · 2009-09-25 08:10:23 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

    stop () { ps -ec | grep $@ | kill -SIGSTOP `awk '{print $1}'`; }
    iridium172 · 2009-12-27 19:40:09 4
  • 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

    tail -n0 -f access.log>/tmp/tmp.log & sleep 10; kill $! ; wc -l /tmp/tmp.log
    dooblem · 2010-04-29 21:23:46 0
  •  1 2 3 >  Last ›

What's this? 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

a function to find the fastest DNS server gives a list of online dns servers. you need to change the country in url (br in this url) with your country code. this command need some time to ping all IP in list.

FLV to AVI with subtitles and forcing audio sync using mencoder
Gives MPEG-4/DivX output video file ready for uploading to YouTube from FLV file downloaded from the site and your own subtitle file UTF-8 encoded. No resizing needed. (?)

Kill all processes belonging to a single user.

Show Directories in the PATH Which does NOT Exist

Recursively remove Mac . (dot) files
Searches from present dir forward and removes all Mac generated . (dot) files.

Every Nth line position # (AWK)
A better way to show the file lines 3n + 1

Trick find -exec option to execute alias
An alias cannot be executed as command in a find -exec line. This form will trick the command line and let you do the job.

Force the script to be started as root
Place this code at the beginning of your script to ensure that it can only be executed by the root.

Get your default route
This gets you your default route programatically, useful for scripts

Loop over the days of a month, in $YYYY$MM$DD format
Edit YYYY and MM at the beginning of the command with the year and month you want. Note that `DD=$(printf "%02d" $d)` will pad single digit integers with a leading zero. Substitute `echo $YYYY$MM$DD` at the end of the line with the command you want to launch, for instance --yyyymmdd $YYYY$MM$DD Also available on GitHub as bash util:

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.


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: