2

This is admittedly a contrived example and it comes as a result of trying to create a series of bash commands that can be executed in such a way that there is no way to resume control of the system other than rebooting.

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
  • 1
    There's an easy way, and a hard way. The easy way involves the syadmin getting a bat, and going for a friendly chat with the user that did that. ;)
    – lcd047
    Commented Jul 15, 2015 at 12:35

1 Answer 1

3

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.

4
  • Thanks, this makes sense / is definitely useful to know. I probably phrased my question too vaguely though. The scenario I was envisioning was a physical box with only one shell instance open (Not using screen or anything) in which the actual line 'while trap '' 2; do read; done;' was the first thing typed by the user after boot and that same user wants to get back to using their machine without restarting the system.
    – pavja2
    Commented Jul 15, 2015 at 13:05
  • In that situation pressing Crtl-Alt-F2 will switch to a new virtual terminal and the process can be killed from there. Even if you disabled all other virtual terminals the Magic SysRq Key could still be used to kill your process.
    – p8952
    Commented Jul 15, 2015 at 13:50
  • Nice. I had no idea about the sysrq key thing - that's exactly what I was hoping existed. If you want to include that in your answer when you get the chance I'll happily accept it for the question in case anyone else ever wonders about this.
    – pavja2
    Commented Jul 15, 2015 at 17:06
  • I've added it to my answer.
    – p8952
    Commented Jul 16, 2015 at 8:30

You must log in to answer this question.

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