8

I frequently (all day every day) have a minicom terminal tab open and execute commands on an embedded Linux system from my Ubuntu laptop. Sometimes I have to execute the reboot command, and sometimes, I am ashamed to admit, I accidentally execute reboot in the wrong tab and my laptop does exactly what it was designed to do without asking any questions...

I am using Ubuntu 16.04 LTS and tried installing molly-guard but that has had no effect.

99 times out of 100 I don't mess up but my laptop takes a good 10 minutes to reboot and I execute reboot frequently enough for this to be an annoyance.

Is there some bit of black magic, I can add to my custom terminal window setup bash script, that will make reboot map to something else (just for that bash session preferably)?

5
  • 7
    How about white magic, such as the incantation of not sitting at a superuser-privileged prompt so frequently an errant reboot will hose your workstation.
    – DopeGhoti
    Feb 23, 2018 at 22:03
  • 1
    Maybe next time I reinstall my OS... Right now all my paths are pretty much reliant on me being root and I am too lazy to fix them. Also there is the horror of having to type 5 extra characters for every other command... Nah defiantly better to spend my time making obscure workarounds and hacks to keep from messing up. :-) Feb 23, 2018 at 22:28
  • That molly-guard isn't working is interesting, since it also implements a safeguard in a similar fashion to other solutions (/usr/sbin/reboot to take over /sbin/reboot). You mention a custom window setup function, does that muck with PATH already?
    – robbat2
    Feb 23, 2018 at 23:16
  • @DopeGhoti Won't necessarily fix things. If you're running a systemd using Linux (which Ubuntu 16.04 is) users who are the only ones logged into the system can restart the machine without elevating privileges (i.e. without sudo/su/logging in as root). I believe there's settings to control this behavior, but many of the single-user-oriented distros (like Ubuntu) turn it on by default.
    – R.M.
    Feb 23, 2018 at 23:33
  • @robbat2 Nope. Here is my path (it matches the one in ~/.bashrc): /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin Feb 24, 2018 at 0:06

2 Answers 2

14

In the ~/.bashrc file on your laptop (not on the embedded machine), add the line:

reboot() { echo  "Hey, don't do that!"; }

If you actually wanted to run reboot on the laptop, you can get around this function by running sudo reboot or /sbin/reboot.


Or, you could make it more friendly, as man0v suggested, by using:

reboot () { echo 'Reboot? (y/n)' && read x && [[ "$x" == "y" ]] && /sbin/reboot; }

I suggest putting such a function in ~/.bashrc because we want it available in interactive bash sessions.

Alternative

One may also want to consider the package molly-guard which is designed to protect machines from accidental shutdowns or reboots. It is available on debian and can be installed via:

apt-get install molly-guard
4
  • 3
    or you could have reboot () { echo 'Reboot? (y/n)' && read x && [[ "$x" == "y" ]] && /sbin/reboot }
    – man0v
    Feb 23, 2018 at 22:07
  • That is probably a better solution than the one I came up with, since mine involves modifying $PATH. Feb 23, 2018 at 22:09
  • 1
    There is a debian package for that: apt install molly-guard
    – Phlogi
    Aug 31, 2022 at 15:19
  • @Phlogi Excellent. I added that to the answer.
    – John1024
    Sep 15, 2022 at 4:53
-2

Okay, after playing around a bit more I found an okay solution:

I created a bash file named 'reboot' in /usr/sbin/ and then added the complete path /usr/sbin/reboot to the front of $PATH.

Here is the script:

read -p "Are you sure? " -n 1 -r
echo    
if [[ $REPLY =~ ^[Yy]$ ]]
then
    /sbin/reboot
fi

Maybe not the best solution though...

6
  • 2
    Very good. One potential issue is that changing the system file /usr/sbin/reboot may cause problems with system utilities as well as X11/GUI programs that depend on the default behavior of /usr/sbin/reboot.
    – John1024
    Feb 23, 2018 at 22:09
  • It also is still letting you keep your nasty habit of leaving superuser prompts open for errant reboot commands.
    – DopeGhoti
    Feb 23, 2018 at 22:10
  • Actually I found I did not even have to type sudo for the reboot command to work, not sure why. Not like I don't leave super user terminals open all the time anyway. I mostly remember to lock my computer before walking away... mostly. :-) Feb 23, 2018 at 22:13
  • 1
    @John1024 no system utilities depend on /usr/sbin/reboot, since there's no such file in a stock system. The stock reboot binary is in /sbin.
    – Ruslan
    Feb 24, 2018 at 6:27
  • 1
    @Kamil there is an accepted answer and this post is 9 months old... Nov 28, 2018 at 23:21

You must log in to answer this question.

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