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
    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 Jul 15 '15 at 12:35
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.

| improve this answer | |
  • 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 Jul 15 '15 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 Jul 15 '15 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 Jul 15 '15 at 17:06
  • I've added it to my answer. – p8952 Jul 16 '15 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.