DEV Community

OnFileNotWanted
OnFileNotWanted

Posted on

2 1

kill processes with fzf

fkill

we can use a shell function, in this case im using zsh

fkill - kill process

fkill() {
  pid=$(ps -ef | sed 1d | fzf -m --ansi --color fg:-1,bg:-1,hl:46,fg+:40,bg+:233,hl+:46 --color prompt:166,border:46 --height 40%  --border=sharp --prompt="➤  " --pointer="➤ " --marker="➤ " | awk '{print $2}')

  if [ "x$pid" != "x" ]
  then
    kill -${1:-9} $pid
  fi
}
select_file() {
    given_file="$1"
    #fd --type file --follow --hidden --exclude .git | fzf --query="$given_file"
    fzf --query="$given_file"
}
Enter fullscreen mode Exit fullscreen mode

or call a script directly

#!/usr/bin/bash

## uncomment lines 4-11 then add to zsh keybinds (default is ctrl+k)
#use fzf to kill proccesses
## fkill_widget() {
## sh $HOME/scripts/fkill     # call fkill script locus
##  zle reset-prompt      
## }
## zle -N fzf-redraw-prompt    # give me my $PS1 back
## zle -N fkill_widget
## bindkey '^k'  fkill_widget    #ctrl+k

################ fkill script start ###########


    if [ "$UID" != "0" ]
    then
        pid=$(ps -f -u $UID | sed 1d | fzf -m --ansi --color fg:-1,bg:-1,hl:46,fg+:40,bg+:233,hl+:46 --color prompt:166,border:46 --height 30%  --border=sharp --prompt="➤  " --pointer="➤ " --marker="➤ " | awk '{print $2}') 
    else
        pid=$(ps -ef | sed 1d | fzf -m | awk '{print $2}') 
    fi
    if [ "x$pid" != "x" ]
    then
        echo $pid | xargs kill -${1:-9}
    fi

############## fkill script end  #############

Enter fullscreen mode Exit fullscreen mode

Download script inline link.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay