LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Killing foo, which I started as foo &. (https://www.linuxquestions.org/questions/linux-newbie-8/killing-foo-which-i-started-as-foo-and-843031/)

stf92 11-08-2010 09:48 AM

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.

GrapefruiTgirl 11-08-2010 09:55 AM

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.

stf92 11-08-2010 11:06 AM

Thanks, most kind GrapefruiTgirl. I referred to a shell built-in command. I must exist. Or a combination of them.

David1357 11-08-2010 11:25 AM

Quote:

Originally Posted by stf92 (Post 4152746)
Now I want to kill it.

Try
Code:

$ kill %1

GrapefruiTgirl 11-08-2010 11:31 AM

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.

Awatto 11-08-2010 11:34 AM

Quote:

Originally Posted by stf92 (Post 4152746)
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>

stf92 11-08-2010 06:46 PM

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.

GrapefruiTgirl 11-08-2010 06:48 PM

Quote:

Originally Posted by stf92 (Post 4153102)
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. ;)

stf92 11-08-2010 07:04 PM

I see in besides the many virtues of LQ, it has that of provinding me fun. I can't stop laughing at my mistake.

Nylex 11-08-2010 10:54 PM

Quote:

Originally Posted by stf92 (Post 4153102)
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.


All times are GMT -5. The time now is 02:58 AM.