LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-08-2010, 09:48 AM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,604

Rep: Reputation: 51
Killing foo, which I started as foo &.


Hi:
I have a process running in the background, started by me in this way:
Code:
$ foo &
I did ^D to stop it:
Code:
[1]+ Stopped
Now I want to kill it. I could use 'ps' to find out the PID and then 'kill'.

OK. But I am almost sure there is a more economical way of killing foo.
Which is it? Thanks.
 
Old 11-08-2010, 09:55 AM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Look up `pkill` and `killall` - they both are quicker than using `ps` first; alternately, you can do something like:
Code:
kill -9 $(ps blah blah blah | awk '{blah blah}')
but still, more typing and less economy than just using killall or similar.
 
Old 11-08-2010, 11:06 AM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,604

Original Poster
Rep: Reputation: 51
Thanks, most kind GrapefruiTgirl. I referred to a shell built-in command. I must exist. Or a combination of them.
 
Old 11-08-2010, 11:25 AM   #4
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by stf92 View Post
Now I want to kill it.
Try
Code:
$ kill %1
 
Old 11-08-2010, 11:31 AM   #5
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Maybe there's something else, but `kill` is what is coming to mind for me.

When you fork a process like that, the PID is returned to you - if you store it (in a variable) then you can later refer to it with `kill` and not need to use `ps`. Look:
Code:
sasha@reactor: foo &
[1] 946
sasha@reactor: kill 946
[1]+  Terminated              foo
sasha@reactor:
or:
Code:
sasha@reactor: foo &
[1] 1126
sasha@reactor: PID=$!
sasha@reactor: echo $PID
1126
sasha@reactor: kill $PID
[1]+  Terminated              foo
sasha@reactor:
So, does there need to be more to it than this (or less?)?

If there's a larger issue at hand here, and this is just a small part of the larger picture, maybe telling us a bit more about the larger problem will allow someone to offer a specific solution.
 
Old 11-08-2010, 11:34 AM   #6
Awatto
Member
 
Registered: Aug 2003
Location: Halifax, NS
Distribution: Debian, Gentoo, Ubuntu, Fedora
Posts: 128

Rep: Reputation: 30
Quote:
Originally Posted by stf92 View Post
Hi:
I have a process running in the background, started by me in this way:
Code:
$ foo &
I did ^D to stop it:
Code:
[1]+ Stopped
Now I want to kill it. I could use 'ps' to find out the PID and then 'kill'.

OK. But I am almost sure there is a more economical way of killing foo.
Which is it? Thanks.

As David1357 said,
Code:
kill %<job number>
will kill the program. Running
Code:
jobs
will show you currently running jobs and their numbers. You can also switch back to a job (ie. a suspended job) by simply typing
Code:
%<job number>
 
Old 11-08-2010, 06:46 PM   #7
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,604

Original Poster
Rep: Reputation: 51
Thank you GrapefruiTgirl for your useful explanation.
P.S: I see you've got a US keyboard, like I do. I say it because the exclamation sign is located above the digit one.
 
Old 11-08-2010, 06:48 PM   #8
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Quote:
Originally Posted by stf92 View Post
Thank you GrapefruiTgirl for your useful explanation.
P.S: I see you've got a US keyboard, like I do. I say it because the exclamation sign is located above the digit one.
Hmm.. Do you say that, because you believe $! to be a typo? If so, it is not: $! is the shell variable which contains the PID of the most recently backgrounded process.
 
Old 11-08-2010, 07:04 PM   #9
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,604

Original Poster
Rep: Reputation: 51
I see in besides the many virtues of LQ, it has that of provinding me fun. I can't stop laughing at my mistake.
 
Old 11-08-2010, 10:54 PM   #10
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by stf92 View Post
P.S: I see you've got a US keyboard, like I do. I say it because the exclamation sign is located above the digit one.
This is not just for US keyboards. Quite a few layouts have that: http://en.wikipedia.org/wiki/Keyboard_layout.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between packages linux-headers-foo and linux-headers-foo-generic? jhwilliams Ubuntu 1 10-19-2009 05:04 AM
Apache site redirects using what rule? foo.site.com -> www.site.com/foo LaughingBoy Linux - Server 2 04-16-2009 09:51 PM
BASH get marked RegEx result: "foo s bar" | /foo(.*)bar/ 1 -> " s " hansschmucker Programming 6 10-23-2007 10:34 PM
Which config file should I use... foo or foo.new? davidguygc Slackware 6 08-01-2007 05:21 PM
what is foo? ashlesha Linux - Newbie 5 07-27-2006 02:09 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration