Interrupt bash infinite while loop with read

Interrupt bash infinite while loop with read

诚然,这是一个人为的示例,它是由于尝试创建一系列 bash 命令而产生的,这些命令的执行方式除了重新启动之外无法恢复对系统的控制。

Basically, I was wondering if there is any way out of this command state, assuming this is the first thing run after boot. To generalize the question, is there an unblockable way to terminate any running command without physically turning off the device?

while trap '' 2; do read; done;

Edit: Just to clarify, I'm interested if this is possible for the same user who ran the command to do within the same shell / assuming the box is inaccessible through anything other than a physical keyboard and this is the first thing that ran after boot.

答案1

Simply sending SIGTERM or SIGKILL via kill -5 <PID> or kill -9 <PID> will cause your process to terminate.

$ cat your_command.sh
while trap '' 2; do read; done;
$ sh your_command.sh
$ ps a | grep 'your_command.sh'
3051 pts/1    S+     0:00 sh your_command.sh
$ kill -5 3051

Even in a situation where the user is completely locked out of the system they can use the Magic SysRq Key. This allows them to send low level commands directly to the kernel and do something like kill all processes except PID 1.

相关内容