0

My goal is to write a script that will show all the running processes and ask the user if they want to kill each process... For each instance of a process with that name, ask the user if they would like to remove it. Remove only the processes the user answers yes (and they must type yes, not just y).

#!/bin/bash

ps

  PID TTY          TIME CMD
 1681 pts/1    00:00:00 tcsh
 1690 pts/1    00:00:00 bash
 1708 pts/1    00:00:00 script
 1710 pts/1    00:00:00 ps

echo "Do you wish to kill PID?"
select yesno in "Yes" "No"; do
case $yesno in
    Yes ) $ kill -9;;
    No ) exit;;

Im not 100% sure how to ask the question to kill each process, and how to have the PID change for each question. I know I have to create a variable, I just don't know exactly how to assign the variable to each PID...

1 Answer 1

1

Here is an example using a for loop:

checkit ()
{
    p="$1"
    echo "Do you wish to kill $p"
    select yesno in "Yes" "No"
    do
        case $yesno in
            Yes) $ kill -9 "$p";;
            No) exit;;
        esac
    done
}

for p in $(ps -o pid=)
do
    checkit $p
done

Check out man ps for details on customizing the ps output. Or: http://linux.die.net/man/1/ps Or: http://unixhelp.ed.ac.uk/CGI/man-cgi?ps

2
  • Thank you so much. I wanted to ask if checkit() is just the name you chose for the function or if that is some integrated tool in Unix?
    – Wifil
    Mar 16, 2015 at 17:21
  • checkit is the name that came to mind. It could have been asdgdsfsas. It is an example of a function. See: tldp.org/LDP/abs/html/functions.html If you want to know if its an existing tool. Open up a terminal and try running checkit in terminal. Should say something like "uhhh no didn't find that tool". You could however turn it into a tool by creating a script named checkit in the /bin or /usr/bin folder, setting the execution bit, and reloading the terminal.
    – jmunsch
    Mar 16, 2015 at 17:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .