Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Ctrl + C doesn't always work to kill the current process (for instance, if that process is busy in certain network operations). In that case, you just see "^C" by your cursor and can't do much else.

What's the easiest way to force that process to die now without losing my terminal?

Summary of the answers: Usually, you can Ctrl + Z to put the process to sleep, and then do kill -9 _process-pid_, where you find the process's pid with ps and other tools. On Bash (and possibly other shells), you can do kill -9 %1 (or '%N' in general) which is easier. If Ctrl + Z doesn't work, you'll have to open another terminal and kill from there.

share|improve this question
    
screen would be a possible solution, Allowing you to create a new window and kill the process from there. –  Bobby Feb 9 '11 at 8:54
2  
assuming you were already in a screen :) –  Dustin Boswell Feb 9 '11 at 8:56
    
Note that you can't kill a process in state D (uninterruptible sleep). –  Mark Johnson Dec 3 '11 at 2:33
add comment

7 Answers

up vote 42 down vote accepted

To understand the problem of why Ctrl + C does not work, it is very helpful to understand what happens when you press it:

Most shells bind Ctrl + C to "send a SIGINT signal to the program that currently runs in the foreground". You can read about the different signals via man signal:

 SIGINT        2       Term    Interrupt from keyboard

Programs can ignore that signal, as they can ignore SIGTSTP as well:

 SIGTSTP   18,20,24    Stop    Stop typed at tty

(Which is what most shells do when you press Ctrl + Z, which is why it is not guaranteed to work.)

There are some signals which can not be ignored by the process: SIGKILL, SIGSTOP and some others. You can send these signals via the kill command. So, to kill your hanging / zombieying process, just find the process ID (PID). For example, use pgrep or ps and then kill it:

 % kill -9 PID
share|improve this answer
1  
A mere remark. Beware that "zombie" is technically a process state, and it is not the same as what you meant here by "zombieying". (A terminated process which has not been wait()-ed by its parent is in zombie (Z) state. In this case, it cannot handle signals anymore.) –  Stéphane Gimenez Sep 19 '11 at 14:15
add comment

If Ctrl-C (SIGINT) doesn't work, try Ctrl-\ (SIGQUIT). Then try Ctrl-Z (and if that doesn't work go to another terminal or SSH session) and do kill -TERM on the process ID. Only as a last resort should you do kill -KILL, a.k.a. kill -9,as it doesn't give the process any chance to abort cleanly, sync its open files, close network connections, etc.

share|improve this answer
add comment

Press Ctrl-Z to suspend the program and put it in the background:

Suspend the program currently running and put it in the background.
This does not stop the process as it does in VMS!

(Restore to foreground again using fg)

Then, you can kill or kill -9 it, given its process ID (you get that from ps a).

share|improve this answer
5  
With bash you can kill $!, as $! is a special variable containing the pid of the last backgrounded program. –  Lloeki Feb 9 '11 at 12:58
    
@Lloeki Doesn't work for me (at least not reliable). I have to bg once before the variable gets a value assigned. –  Daniel Beck Feb 9 '11 at 13:10
    
@Daniel, correct. It is as I said the last backgrounded process, thus needs bg as right after Ctrl+Z it is merely suspended. –  Lloeki Feb 9 '11 at 13:22
1  
Note: this is not just a trick, using ps output (or killall) is quite risky if you have multiple processes with the same name running. ps -e -o pid,command will provide pid+full args, not just program name, but again might not be enough to discriminate. In contrast $! is a sure hit. –  Lloeki Feb 9 '11 at 14:07
1  
@Lloeki I disagree. Example output line from ps a on my system: 27721 s000 T 0:00.09 top How many suspended (T, I think) instances of the same command (top) do you have running in the same tty (s000)? –  Daniel Beck Feb 9 '11 at 17:30
show 2 more comments

Usually, you can still stop the process (Ctrl + Z) and then use kill -9. For kill -9, you need the process PID first. For background jobs, kill -9 %1 is easiest way to do it - if you are unsure what is the number of background job you want to kill, run jobs.

Alternatively, you can find process ID with

ps

Then you can run

kill -9 <Appropriate PID from ps output>
share|improve this answer
add comment

See this link as well.

Ctrl+Z: pause a process.

Ctrl+C: politely ask the process to shut down now.

Ctrl+\: mercilessly kill the process that is currently in the foreground

share|improve this answer
add comment

1) If you are on the console and in multi-user mode, you could press CTRL-ALT-Fn and login on another screen, use ps -ef | grep <myprocessname> or pidof <myprocessname> and then kill -9 the process by ID number.

2) If you are connected remotely, do the same via another terminal session.

You could also make life a little easier by installing htop, which is a more versatile version of top that allows you to selectively kill running processes. Most distros have htop in a repo.

3) if you are just stuck in a hung ssh session (to another system, for example), try pressing tilde (~), which is the escape key, and then press CTRL-Z to drop back to the host session, then you can kill the stuck ssh process or wait for it to timeout, which most sill do after a period of inactivity.

share|improve this answer
add comment

A simpler solution for Bash (and other shells?) is to do:

Ctrl-z      followed by     kill -9 %1

where '%1' refers to the job number being killed. It might be '%2' (or something else) if you already have other jobs sleeping. You can see which job number it is when you hit Ctrl-z:

[1]+  Stopped                 <process name>

Note that 'kill' is the shell's version of kill, not /bin/kill.

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.