Home Man
Search
Today's Posts
Register

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.

Tags
command, kill, shell scripts

👤 Login to reply

 
Thread Tools Search this Thread
# 1  
Old 04-21-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

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 01:32 AM.. Reason: added code tags
# 2  
Old 04-21-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

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 01: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.

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 03: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 to reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Kill a program from bash arpagon UNIX for Dummies Questions & Answers 7 07-25-2014 01:34 PM
Kill a running program? petel1 UNIX for Dummies Questions & Answers 3 05-26-2010 07:05 AM
Kill shell script when host program not running/disk unmounted pcwiz Shell Programming and Scripting 2 10-02-2009 10:04 AM
Kill the running program akhtar.bhat Shell Programming and Scripting 1 12-26-2008 06:48 AM
Script to open program and send/execute command in program tuathan UNIX for Dummies Questions & Answers 1 11-19-2008 08:59 PM
Kill a process without using kill command sudhamacs Linux 6 10-27-2008 12:36 PM
Kill a process without using kill command sudhamacs Shell Programming and Scripting 0 10-01-2008 05:19 PM
kill multiple instances of the same program ipzig Shell Programming and Scripting 12 12-17-2007 01:42 AM
kill textrdit program jeter Programming 6 06-07-2006 09:12 AM
C program to kill root processes TioTony Programming 8 07-24-2003 11:16 PM


All times are GMT -4. The time now is 05:17 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password