How to terminate a background process?
Solution 1
There are many ways to go about this.
Method #1 - ps
You can use the ps
command to find the process ID for this process and then use the PID to kill the process.
Example
$ ps -eaf | grep [w]get
saml 1713 1709 0 Dec10 pts/0 00:00:00 wget ...
$ kill 1713
Method #2 - pgrep
You can also find the process ID using pgrep
.
Example
$ pgrep wget
1234
$ kill 1234
Method #3 - pkill
If you're sure it's the only wget
you've run you can use the command pkill
to kill the job by name.
Example
$ pkill wget
Method #4 - jobs
If you're in the same shell from where you ran the job that's now backgrounded. You can check if it's running still using the jobs
command, and also kill it by its job number.
Example
My fake job, sleep
.
$ sleep 100 &
[1] 4542
Find it's job number. NOTE: the number 4542 is the process ID.
$ jobs
[1]+ Running sleep 100 &
$ kill %1
[1]+ Terminated sleep 100
Method #5 - fg
You can bring a backgrounded job back to the foreground using the fg
command.
Example
Fake job, sleep
.
$ sleep 100 &
[1] 4650
Get the job's number.
$ jobs
[1]+ Running sleep 100 &
Bring job #1 back to the foreground, and then use Ctrl+C.
$ fg 1
sleep 100
^C
$
Solution 2
In bash you can use fg
to get the job to the foreground and then use Ctrl+C
Or list the process in the background with jobs
and then do
kill %1
(with 1 replaced by the number jobs
gave you)
Solution 3
You can equally use kill $!
to kill the most recently backgrounded job.
Solution 4
EDIT: Once in the foreground, you can Ctrl+C, or as @Zelda mentions, kill with the '%x' where 'x' is the job number will send the default signal (most likely SIGTERM in the case of Linux).
just type fg
to bring it to the foreground, if it was the last process you backgrounded (with '&').
If it was not the last one, type: jobs
and find the 'job number', represented in '[]'. Then just type:
fg 2
..where '2' is the job number, for example:
foo@bar:~/junk/books$ jobs
[1]+ Running okular how_to_cook_a_turkey.pdf &
foo@bar:~/junk/books$ fg 1
okular how_to_cook_a_turkey.pdf <- this is now in the foreground.
Solution 5
One thing I don't see here, which I've found very useful especially when testing out commands, is pidof
. You can use pidof [command]
to find the process id of a process that is currently running. I like it because it allows me to quickly find the id of the command I want, which is usually something I just invoked.
Once you have the pid, you can simply kill the process. It allows for creating simple scripts for killing a process only if it's currently running.
Related videos on Youtube
Comments
-
Mohammad Etemaddar over 1 year
I have started a wget on remote machine in background using
&
. Suddenly it stops downloading. I want to terminate its process, then re-run the command. How can I terminate it?I haven't closed its shell window. But as you know it doesn't stop using Ctrl+C and Ctrl+Z.
-
Mohammad Etemaddar over 10 yearsThe
jobs
has nooutput
andfg
sais:-bash: fg: 1: no such job
. But typingfg
works well and alsopkill wget
works well. butps -eaf|grep wget
and thenkill <process number>
dose not stop the job. ps: I use the third number as process number. -
slm over 10 years@MohammadEtemaddar - use the 2nd number from the output of
ps
. The 3rd # is the parent's process id. -
Mohammad Etemaddar over 10 yearsWhen I use the second number, It sais:
No such process
and then when I check the process number again usingps -eaf|grep wget
I find that the process number has changed. -
Mohammad Etemaddar over 10 years
-
slm over 10 years@MohammadEtemaddar - ah, the ps is finding the grep. Do it like this:
ps -eaef| grep [w]get
. -
slm over 10 years@MohammadEtemaddar - you can also use
pgrep
instead,pgrep wget
. -
Mohammad Etemaddar over 10 yearsWould you please tell a little about the functionality of option
eaef
? -
slm over 10 years@MohammadEtemaddar - sorry the extra e is a typo. Should read
ps -eaf | grep [w]get
. The options are are in theps
man page.man ps
. -
John_West over 8 yearsSuddenly, on RHEL5 my job was not killed by
Ctrl-C
afterfg
(method 5 did not work.). Method 4 seems to be the fastest and the shortest to type. -
mejdev about 7 yearsAh this helped me! kill %1 wasn't working for some reason, "kill: failed to parse argument: '%1'" and Ctrl+C wasn't working; couldn't find process in ps or anything because it was root (I forgot I used sudo with it)
-
Jack Chi almost 6 yearsIt's
fg %1
to bring the job back into foreground. -
slm almost 6 years@JackChi - the
fg 1
notation works as well. Try it. -
Alex over 5 yearsI did the first, second and third steps, it didnt work, so I just closed the current open tag where I opened the programs, and problem solved
-
Nate Ritter over 4 yearsIf it's used as a command like an alias in a bash profile, be sure to wrap in single quotes.
-
Ronny Sherer over 4 years
kill %%
kills the last background process. Repeating it will kill the one before and so on.kill %1
kills the first background process. -
Fox about 3 yearsThis adds nothing to the currently-accepted answer, and moreover won't work in this case. Using
kill %1
(as suggested by others) will, but Ctrl+C is meaningless against programs that ignore the keyboard interrupt as mentioned in this question -
avm about 2 yearswhat does [ ] do in grep [w]get?
-
slm about 2 years@avm - it basically tricks
grep
into not showing the actualgrep
command in the output. Thegrep
command shows up asgrep [w]get
but the commandgrep
is looking for the stringwget
. The[...]
is a set of characters, in this case a set of 1 character,w
. -
Admin almost 2 yearsBut if you launched this process yourself, you should already have the pid of this background process.