Trying to kill a program via help command.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trying to kill a program via help command.
# 1  
Old 04-22-2008
Error 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 Smilie

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

Code:
ps > ps.txt

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

Code:
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
Code:
KILLPID=$1

then after

Code:
kill $KILLPID

but that won't work.

(using bash)

Last edited by Yogesh Sawant; 04-22-2008 at 02:32 AM.. Reason: added code tags
# 2  
Old 04-22-2008
i have also tried
Code:
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 Smilie

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

Last edited by Yogesh Sawant; 04-22-2008 at 02:31 AM.. Reason: added code tags
# 3  
Old 04-22-2008
How you Try to kill a program via help?
Code:
ps -aux | awk '/[p]rogram/{print $2}'| xargs kill -15

# 4  
Old 04-22-2008
-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. Smilie

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} '

Last edited by TiznaraN; 04-22-2008 at 04:57 AM..
# 5  
Old 04-22-2008
If 'ps -aux' don't work for you, check ps man pages for your environment.
Code:
ps | awk '/[p]rogram/{print $1}'| xargs kill -15

# 6  
Old 04-22-2008
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:

Code:
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).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Kill a program from bash

to kill a program in bash, for instance 'mousepad' I use kill $(pidof mousepad); or pkill mousepad But it only works if we use another bash window; If it is started from the same bash, that does not work: #mousepad;kill $(pidof mousepad); In this case, it looks like mousepad hangs... (7 Replies)
Discussion started by: arpagon
7 Replies

2. UNIX for Dummies Questions & Answers

Kill a running program?

Hello i'm trying to make a program which can find a running program on the system and kill it, then repeat that every 5 minutes. The name of the process is given with an argument. I have done this so far, but still not working. if $1 in $(ps -e) ; then kill $(pidof $1) sleep 5m fi (3 Replies)
Discussion started by: petel1
3 Replies

3. Shell Programming and Scripting

Kill shell script when host program not running/disk unmounted

I have my Mac OS X program executing a shell script (a script that copies files to a drive). I want to make it so that the shell script automatically kills itself if it finds that the host .app is not running OR kill itself if the drive that it is copying files to has been unmounted. Right now what... (2 Replies)
Discussion started by: pcwiz
2 Replies

4. Shell Programming and Scripting

Kill the running program

Dear All, I have a script which after executing is not stoping when i press ctrl+c. Now i want to Append the script in such a way when i press ctrl+c while execution of the program it should take it as arguements and should kill the script/running program forcefully. I know the command to... (1 Reply)
Discussion started by: akhtar.bhat
1 Replies

5. UNIX for Dummies Questions & Answers

Script to open program and send/execute command in program

Hi, i want to write a script that executes a program (exec?) . this program then requires a filename as input. how do i give it this input in the script so the program will be complete run and close by the script. e.g. exec prog.exe program then asks for filename "enter filename:"... (1 Reply)
Discussion started by: tuathan
1 Replies

6. Linux

Kill a process without using kill command

I want to Kill a process without using kill command as i don't have privileges to kill the process. I know the pid and i am using Linux 2.6.9 OS. (6 Replies)
Discussion started by: sudhamacs
6 Replies

7. Shell Programming and Scripting

Kill a process without using kill command

Sorry, posted the question in other forum. (0 Replies)
Discussion started by: sudhamacs
0 Replies

8. Shell Programming and Scripting

kill multiple instances of the same program

Hi, I know that the answer to this is very simple, since I saw somebody do it some time back..but I forgot how. The problem is, I have multiple instances of the same program running simultaneously and I want to kill them all in a single command. I know that it can be done using awk '{print... (12 Replies)
Discussion started by: ipzig
12 Replies

9. Programming

kill textrdit program

Dear All: I use sun OS system and write a code in c as folloing purpose kill textedit program,but i get some error please give me a great help Thanks. #include <stdlib.h> int main() { const char cTestPrag=" kill -9 `ps -ef | grep textedit | grep -v "grep"| awk '{print $2}'| xargs` "; ... (6 Replies)
Discussion started by: jeter
6 Replies

10. Programming

C program to kill root processes

Hello, First let me start by saying I have searched the forum and read all the SUID stuff but it is not in the neighborhood I am looking for. Here is the problem. We want to grant a non super-user permission to kill root processes but only if the process matches certain criteria. ... (8 Replies)
Discussion started by: TioTony
8 Replies
Login or Register to Ask a Question