5

I made the mistake of running the command:

cat /dev/urandom

And the console (hosted on Windows) filled with junk and stopped responding to Ctrl-Z or anything else.

Is there any way I could have recovered the console from this?

Tejas Lotlikar
  • 2,945
  • 5
  • 17
  • 26
  • Can't you just close WSL and reopen it? That should kill it, considering you can't run a FTP server or any type of server in WSL and close the window as it will kill any running app. Normally it is Ctrl+C to break a command. – Terrance Dec 28 '20 at 04:57
  • 1
    Alternatively open another WSL sesstion, ps -ef | egrep cat and then kill -9 the processid of the cat process. – cup Dec 30 '20 at 15:47
  • Just ran into the same situation. Window does not respond, task manager does not end the task, kill -9 does nothing. I guess nothing but reboot will help. – comodoro Jan 21 '22 at 19:05

1 Answers1

1

First of all I want to highlight there is a significant difference between ctrl+z and ctrl+c:

  • ctrl+z sends SIGTSTP to a foreground process, effectively putting it in the background - it is not appropriate in this case.
  • ctrl+c sends SIGINT which will interrupt the process and this is what you should try first.

More about the signals you can read at man 7 signal.

Sometimes just closing the terminal window is not the best solution because you will lost the recent history which could be important for you.

By opening another terminal window (or TTY at real Linux) you can try to kill the cat process by either of the following commands.

killall -s 9 cat

The command killall kills processes by name and it will try to kill all cat processes. The option -s 9 will send SIGKILL instead SIGTERM which could be read as force kill. In most cases just killall cat is enough.

kill -9 $(ps -e | awk '/cat/ {print $1}')

The command kill will send a signal to a process and by the option -9 we will send SIGKILL as in the above command, but here we must provide PIDs (process identifiers) instead of names. The command substitution $() will provide a list of all cat processes running on the system. Here is haw:

  • ps -e will list all running processes on the system,
  • this list will be piped | as input to the awk command,
  • finally awk '/cat/ {print $1}' will filter the list by printing the first field $1 of each line that contains the string cat. This first field contains the PID of the relevant process.

Note in both commands you do not need to use sudo because normally your user should be the owner of the cat process(es).

pa4080
  • 29,831