How to kill a runaway cat?

Many times I accidentally run the cat command on files that have contents up to few thousand lines.

I try to kill the cat command with Ctrl+ C or Ctrl+Z, but both only take effect after the total output of cat is displayed in the terminal, so I have to wait till cat gets completely executed.

Is there a better solution that avoids waiting? Because sometimes the files are up to size of 100MBs, and it gets irritating to wait for it.

I am using tcsh.

Asked By: JigarGandhi

||

You can try with this

Reset the display use ctrlj

If cat is still running then open a new terminal and run this pkill -9 -f cat ” This will kill all cat processes. “

Answered By: vembutech

If the file(s) in question contain lots of data, sending the signal can actually get to cat before it finishes. What you really observe is the finite speed of your terminal – cat sends the data to the terminal and it takes some time for the terminal to display all of it.

Remember that it usually has to redraw the whole output window for each line of output (i.e. move the contents of the window one line up and print the next line at the bottom). While there are techniques and algorithms to make this faster than if it was done the straightforward way, it still takes some time.

Thus, if you want to get rid of the output as quickly as possible, hide the terminal window, because then (usually) no actual redrawing takes place. In a graphical environment this can mean either minimizing the window or switching to a different virtual desktop, on the Linux virtual console just switch to another one ((Ctrl +)Alt + Fx).

Also notice that if you ran this over a slow network link (SSH over a GSM connection, for example), you would definitely see much less output before cat is killed by the signal, because the speed of the terminal redrawing wouldn’t be the bottleneck any more.

Answered By: peterph

When using bash, you can suspend the job using Ctrl+Z and then kill it, either using the PID or by following the answers from https://stackoverflow.com/questions/1624691/linux-kill-background-task (e.g. kill -9 %%)

Answered By: Jannis

open a new console, for example by clicking onto the console icon or by pressing Ctrl+Alt+F2. There, enter

killall -9 cat

and cat will be killed unless you have a stale NFS lock or an “uninterruptible sleep” situation (read on a bad sector with no timeout set).

Answered By: Thorsten Staerk

Terminal Setup

I reckon this is more to do with the way the terminal is set up, than with any buffering issue. Check the output of stty -a | grep intr, you should have intr = ^C; on the output line if CtrlC is enabled at the tty/pty. If it isn’t, you can use stty intr ^C to enable it. Add the line to your .tcshrc or .login to make it permanent (or delete the line that changes it in the first place!).

Failing CtrlC, you can also try sending SIGQUIT with Ctrl. If this doesn’t work, again check stty -a | grep quit to see if it is properly set up.

Terminal Emulator Setup

Also check the settings for your terminal emulator (if you are using one), it may be that there is a shortcut set up at this level (maybe for copy or something) and the CtrlC does not reach the pty level. A shortcut could also be set up somewhere else in your desktop environment or Window system.

A good test on Linux if you are using a terminal emulator is to switch to a Linux console (CtrlAltF1), login there and see if the same behaviour occurs. If it doesn’t then this suggests the problem lies with your Window system or terminal emulator.

It could be an issue with the delay between data being read from the pty device as suggested by peterph. But if this is the case and you do actually have to wait minutes for the data to be displayed, then surely the terminal emulator is buffering way too much data (or your PC is very slow). The answer would be to find a way to reduce that buffer size in your terminal emulator settings or use a different one.

Extra Tip

Something else worth adding; I usually end up in the runaway cat situation when if I accidentally cat a binary file. The other effect of this can be to screw up your terminal settings (if the binary data happens to match various terminal escape codes which it often does). If tput is installed (usually is by default), you can avoid having to restart with the following command:

tput reset
Answered By: Graeme

This is what tmux option c0-change-interval and c0-change-trigger designed for. You should use a screen manager for resumable session anyway.

Answered By: J-16 SDiZ

The CtrlO option has been in Unix since the 110 baud days. Runaway cat commands were always a problem when a long ASCII file was dumped at the slow output device and the the entire file placed in the device driver output buffer. CtrlO would start the flush of the buffer and a follow up CtrlO would toggle off the flush so the cat could be read at normal speed. CtrlO was entered and the entire file would flush and quickly return a command prompt.

Whether or not Linux device driver coders felt the need to continue to add this feature is unknown. This feature was added at the device driver level where CtrlS and CtrlQ could only be implemented. I used this to dump large debugging runs with lots of check print and then scroll down to the place I needed to see.

Answered By: user62612
Categories: Answers Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.