Trying to kill a program via help command.

Hi all, I have a program and I'm trying to kill it. I'm probably going the longest way around of doing it but now that I've tried to get this to work for a few hours I'd appreciate some help :slight_smile:

So the program has an infinite loop which keeps it going, as of right now I just simply if exit then $SHELL to get back to prompt, but the program remains.

So the way I've gone about this is by each time exit is run I have

ps > ps.txt

(suppose I could just pipe ps to the awk, haven't tried that yet)

awk '/program/ {print $1}' ps.txt

this is printing the processes ID, now I'm trying to store that to a variable and then kill the variable is that possible? Only thing I can think of trying is replace print with

KILLPID=$1

then after

kill $KILLPID

but that won't work.

(using bash)

i have also tried

ps | kill | awk

I get kill error and then the PID prints at the end =(

edit:

also if I manually kill the process after, the shell closes...if there's a much easier way of doing this and my way isn't possible let me know too :slight_smile:

I haven't done any programming in about 6 years and I'm very new to UNIX.

How you Try to kill a program via help?

ps -aux | awk '/[p]rogram/{print $2}'| xargs kill -15

-aux errors for me, here's what I have now

ps | awk ' /program/ {print $1} '

This is outputting the PID, isn't there a way to store the number in a variable rather than print it, maybe then pipe it into a kill? I'm not sure of the syntax I guess.

edit: I meant to say via exit command in the title. :rolleyes:

edit2: no longer relevant

edit3: break; I gotta start dumbing myself down, or reading more before trying to code.

But, pretend this was to do something other than break a loop., Let's say I am trying to find out how many times someone has logged into the machine. Use last to display all the different logins, several names pop up, some multiple times. This is something else I was working on, all I can figure out is the total number of logins, but I can't determine how to compare if $1(user name) is there more than once, and ontop of that count it if it is.

ex.

toyoung
rasmith
toyoung

output i'm looking for would be

toyoung 2
rasmith 1

here's what i have:

last | awk ' END {print "Number of logins:", NR} '

If 'ps -aux' don't work for you, check ps man pages for your environment.

ps | awk '/[p]rogram/{print $1}'| xargs kill -15

Just for clarification, if you are only going to use it once, there is no need to capture it in a variable.

Of course, the kill command expects a command-line argument, not standard input; xargs arranges things suitably, so you could say that's how you "pipe it to kill".

Equivalently, you could use backticks:

kill `ps aux | awk '/[p]rogram/ { print $1 }'`

Notice, those are backticks (ASCII 96), not regular quotes.

You should probably start a different thread about your "last" question, but basically, the idiom for counting things is "pipe to | sort | uniq -c" (and maybe | sort -rn to top it off, to get the ones with the biggest number of hits first).