Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Trying to kill a program via help command.

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-22-2008   -   Original Discussion by TiznaraN
TiznaraN TiznaraN is offline
Registered User
 
Join Date: Mar 2008
Last Activity: 22 April 2008, 10:55 PM EDT
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
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 Linux

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
Sponsored Links
    #2  
Old Unix and Linux 04-22-2008   -   Original Discussion by TiznaraN
TiznaraN TiznaraN is offline
Registered User
 
Join Date: Mar 2008
Last Activity: 22 April 2008, 10:55 PM EDT
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
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 Linux

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
Sponsored Links
    #3  
Old Unix and Linux 04-22-2008   -   Original Discussion by TiznaraN
danmero danmero is offline Forum Advisor  
 
Join Date: Nov 2007
Last Activity: 31 July 2016, 9:42 AM EDT
Location: H3X
Posts: 2,163
Thanks: 11
Thanked 123 Times in 116 Posts
How you Try to kill a program via help?

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

    #4  
Old Unix and Linux 04-22-2008   -   Original Discussion by TiznaraN
TiznaraN TiznaraN is offline
Registered User
 
Join Date: Mar 2008
Last Activity: 22 April 2008, 10:55 PM EDT
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
-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. Linux

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..
Sponsored Links
    #5  
Old Unix and Linux 04-22-2008   -   Original Discussion by TiznaraN
danmero danmero is offline Forum Advisor  
 
Join Date: Nov 2007
Last Activity: 31 July 2016, 9:42 AM EDT
Location: H3X
Posts: 2,163
Thanks: 11
Thanked 123 Times in 116 Posts
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

Sponsored Links
    #6  
Old Unix and Linux 04-22-2008   -   Original Discussion by TiznaraN
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 11 Times in 9 Posts
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).
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Kill a running program? petel1 UNIX for Dummies Questions & Answers 3 05-26-2010 08:05 AM
Kill the running program akhtar.bhat Shell Programming and Scripting 1 12-26-2008 07:48 AM
kill multiple instances of the same program ipzig Shell Programming and Scripting 12 12-17-2007 02:42 AM
kill textrdit program jeter Programming 6 06-07-2006 10:12 AM
C program to kill root processes TioTony Programming 8 07-25-2003 12:16 AM



All times are GMT -4. The time now is 01:45 AM.