Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have started a wget on remote machine in background using &. Suddenly it stops downloading. I want to stop its process, then start it again. How can I stop it?

I haven't close its shell window. But as you know it doesn't stop using Ctrl+C and Ctrl+Z.

share|improve this question
add comment

3 Answers

up vote 14 down vote accepted

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
$
share|improve this answer
    
The jobs has no output and fg sais: -bash: fg: 1: no such job. But typing fg works well and also pkill wget works well. but ps -eaf|grep wget and then kill <process number> dose not stop the job. ps: I use the third number as process number. –  Mohammad Etemaddar Dec 12 '13 at 7:32
1  
@MohammadEtemaddar - use the 2nd number from the output of ps. The 3rd # is the parent's process id. –  slm Dec 12 '13 at 7:34
1  
@MohammadEtemaddar - ah, the ps is finding the grep. Do it like this: ps -eaef| grep [w]get. –  slm Dec 12 '13 at 7:54
1  
@MohammadEtemaddar - you can also use pgrep instead, pgrep wget. –  slm Dec 12 '13 at 7:56
1  
@MohammadEtemaddar - sorry the extra e is a typo. Should read ps -eaf | grep [w]get. The options are are in the ps man page. man ps. –  slm Dec 12 '13 at 8:01
show 4 more comments

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)

share|improve this answer
add comment

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.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.