14

I'm looking for someway to stop the output to STDOUT when I realize a command is wrong. For instance, if I accidentally cat a very large file. Usually it will eventually stop if you hold down ctrl + c, but it takes a long time.

Is there a way to stop the output more immediately?

4 Answers 4

13

You can run ctl-z to background it and then kill the PID.

$ ctl z (to background it)

$ kill -9 {pid} 'of process that just got backgrounded'

1
  • 1
    Why are you kill -9ing if it isn't absolutely necessary?
    – benjwadams
    Nov 3, 2014 at 18:44
4

Often the data has already been sent to the display, and the "long time" is the terminal trying to display the data. Doing anything to the task won't help (i.e., Ctrl+Z or Ctrl+C), you'll need either a faster terminal or something to buffer the data.

I pipe large outputs through less and tail depending on whether I want to view the top or the bottom of the file, since both allow me to easily control the amount of data being sent to the terminal and close the program if it's not what I want.

4

You should be able to pipe it to more or less

cat large_file.txt | less

At which point you could page through the answer and break out with a :q

You could also use grep to help sort through your answer if you're looking for a specific error/problem.

http://www.westwind.com/reference/os-x/commandline/pipes.html

2
  • 2
    CHK asking about situation with ALREADY started process. You can't use less/more in this case.
    – september
    Jul 2, 2013 at 15:55
  • 1
    While not an answer to the direct question, this has value in that it tells how to avoid the situation before it happens.
    – TecBrat
    Aug 7, 2014 at 15:34
1

Switching to a different Terminal (via CTRL+F1 to F6 in text mode, or opening a new window in screen via CTRL+A,C) after having pressed either CTRL+Z or CTRL+C will cause the original terminal to skip trying to do all the linefeeds and directly jump to the last line of output in a shorter time

2
  • Does this work when the "long time" is the output going into a network buffer and taking a long time to transfer all the text to the receiving terminal? If so, how does one accomplish the same in, say, terminator? I switch to another tab and nothing. On the other hand, if I kill the ssh session using ~. then it immediately stops. I'd rather not kill the session though.
    – vontrapp
    Dec 29, 2018 at 3:11
  • @vontrapp Good question (which is probably worth a new question). My guess would be that won't work, but over SSH for long running processes using tmux or screen is always a recommendation Dec 29, 2018 at 10:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .