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.

In my attempt to get unique entries (read lines) out of a simple text file, I accidentally executed nano SomeTextFile | uniq.

This "instruction" renders the shell (bash) completely (?) unresponsive/non-usable -- tested within from Yakuake and Konsole. I had to retrieve the process id (PID) (by executing ps aux | grep nano) and manually sudo kill -9 the PID in question.

Anyhow, couldn't (or shouldn't?) the above return some error message? Why doesn't Ctrl+C kill this pipeline? Is there an easier or cleaner way to stop it than kill -9?

share|improve this question
    
Same thing happens with nano xyz > abc. Similar with vim –  Karthik T Dec 20 '12 at 5:15
1  
Not really sure what your question is. You could have suspended the job with Ctrl+Z, then kill %1 to get rid of it... –  ire_and_curses Dec 20 '12 at 5:17
1  
@ire_and_curses You can't; nano catches SIGSTOP too –  Michael Mrozek Dec 20 '12 at 7:20
1  
@ire_and_curses @Michael it seems, indeed, that Ctrl+Z and kill %1 does something, i.e. it returns Received SIGHUP or SIGTERM. Searching for the process using ps aux | grep nano returns [1]+ Terminated nano head_of_data_url_script_2012-12-17_112253.txt | uniq. So, Ctrl+z is a solution, right? –  Nikos Alexandris Dec 20 '12 at 8:38
    
I guess @cjh explained very well what happened in his answer. –  Nikos Alexandris Dec 20 '12 at 8:42

4 Answers 4

up vote 2 down vote accepted

As other answers have already explained, Ctrl+C doesn't kill Nano because the input of nano is still coming from the terminal, and the terminal is still nano's controlling terminal, so Nano is putting the terminal in raw mode where control characters such as Ctrl+C are transmitted to the program and not intercepted by the terminal to generate signals.

When intercepted by the terminal, Ctrl+C generates a SIGINT signal. If you know the process ID of nano (you can find out with ps u -C nano (Linux ps syntax) or pgrep nano or other process listing utility), you can send this signal with kill -INT 12345 where 12345 is the PID. However, SIGINT conventionally means “return to main loop”, and Nano doesn't exit when it receives SIGINT. Instead, send SIGTERM, which means “terminate gracefully”; this is the default signal, so you can just run kill 12345. Another possibility is kill -HUP 12345; SIGHUP means “you no longer have a terminal, quit gracefully unless you can live without”. If all else fails, send SIGKILL (kill -KILL 12345, or famously kill -9 12345), which kills the program whether it wants to die or not.

Many programs, including Nano, recognize Ctrl+Z to suspend. This is the same sequence that sends the SIGTSTP signal. If the program recognizes this control key, you get back a shell prompt, and since the program becomes a background job, you can easily kill it with kill %% (which sends a signal to the job that has last been put into the background).

With Nano, there is an alternate way: send it its exit key sequence, i.e. Ctrl+X followed if necessary by N for “don't save”. But as a general matter, remember this:

  1. Try Ctrl+Z followed by kill %%, and if this doesn't kill the program kill -9 %%.
  2. If Ctrl+Z didn't work, switch to another terminal, find out the process ID (you can use ps -t pts/42 to list the processes running on the terminal /dev/pts/42) and kill it.
share|improve this answer

The pipe symbol is sending the ouput of nano to the input of the uniq command.

However, nano is still running and receiving your keyboard input. It's just that you can't see its ouput as uniq won't ouput it's results until it sees an End-Of-File (this isn't important to the question).

When you press ctrl-c it is 'caught' by nano, but it does not cause it to exit.

The way to exit nano is to press ctrl-x, however nano will not exit if modifications have been made, for example, if you have pressed any other keys since entering. In this case, nano will prompt you with the following:

Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES) ?                                                                
 Y Yes
 N No           ^C Cancel

Pressing the 'n' key will exit without saving changes.

It is possible that you hit a few key combinations before realising you were in nano, in which case you may be a few menus deep. Thankfully in nano pressing ctrl-x will also exit from most menus (but will not cancel the above prompt), this means pressing ctrl-x multiple times will never take you further away from the exit.

If you are 'trapped' a few menus 'deep' you can usually get out by pressing ctrl-x a few times, followed by the n key.

share|improve this answer
    
Well explained. Ctrl+Z and kill %1 as suggested in a comment to the initial question by @ire_and_curses is not required. –  Nikos Alexandris Dec 20 '12 at 8:41
    
Apologies, I think @Gilles' answer deserves to be the accepted one -- so I will transfer the accepted status. Mea culpa, I should have given more time before accepting an answer. –  Nikos Alexandris Dec 21 '12 at 6:11

The shell doesn't receive you Ctrl+C, nano does. Programs are able to define custom behavior for interrupt signals and nano is one such program. To see what the custom behavior is open nano without the redirection and send it a Ctrl+C.

share|improve this answer

Strike Ctrl+X.

Nano captures all keyboard input and inside Nano Ctrl+C has an entirely different meaning than in the shell. If you run a nano in a separate window, you can easily see what keys you have to enter to exit. Ctrl+X

share|improve this answer

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.