4

I am trying to edit the reboot command to echo a message to the user, but the command itself located in /sbin is compiled. Is it possible to edit the command and have it print something out when the user types reboot?

  • You'd have to be more precise about what you mean by "print something out". – fkraiem Jun 19 '16 at 18:30
  • You can make your reboot script and apply it! – Pandya Jun 20 '16 at 11:28
12

Yes, but you would have to edit its source code, add your warning, recompile and replace the executable with the modified one.

A much simpler approach would be to create a script that prints your warning and then calls reboot and save that script as reboot:

  1. Create a file with this content and save it as /usr/bin/reboot:

    #!/bin/sh
    echo "Eeeek! You are rebooting!"
    /sbin/reboot
    
  2. Make it executable:

    sudo chmod a+x /usr/bin/reboot
    

Now, when you run reboot, because /usr/bin is before /sbin in your $PATH, you will run the script which will print the warning and then run reboot.

5

One way to do it as terdon shows - with a wrapper script.

The other approach is via function. Function names have priority over system commands and aliases, which means that if we create function reboot, it will be executed. What's convenient is that you don't have to create separate file, you can place it into /etc/bash.bashrc for all users or ~/.bashrc only for yourself.

As for the function itself, we could do something like this

reboot()
{
   printf "Reboot is about to happen , press enter "
   read
   /sbin/reboot
   # or for those who like to use systemd way, you can do systemctl reboot
}

What's also cool is that you can press Ctrl+C to cancel out of this function

1

The easiest way to modify a command is to make an alias that shadows it and calls it either with different arguments or before/after doing something else.

You create an alias like this:

alias ALIASNAME='COMMAND'

Don't worry about giving the alias the same name as a command it consists of, you can not generate recursion here.

So to first print a message, wait a few seconds and finally reboot, this command is suitable:

echo "Rebooting in 10 seconds - abort with CTRL+C" && sleep 10 && reboot

Now we need to alias this long command to reboot, so that it will be run instead of the original /sbin/reboot one:

alias reboot='echo "Rebooting in 10 seconds - abort with CTRL+C" && sleep 10 && reboot'

This alias is now valid for your current user only, and only for this Bash session. Closing the terminal window will make it vanish again.

To make it persistent for your user account, we must ensure that Bash runs it every time it starts a new session. This is most easily done by appending it to your ~/.bash_aliases file. Open it with your favourite text editor. If it does not exist yet, simply create it.

Append the alias reboot='...' command from above to the end of the file. If you want, you can also add a comment describing what this is about. Comment lines must start with a # character. Save the file after you finished the edit.

When you now open a new terminal window, the alias will work.


Please note that this approach is for 16.04, where (for whatever reason) /sbin/reboot does no longer need to be run as root, but is also happy with normal user privileges. If you use an older release where /sbin/reboot still needs to be run as root or with sudo, you must add the sudo inside the alias command:

alias reboot='echo "Rebooting in 10 seconds" && sleep 10 && sudo reboot'

Running the alias itself with sudo (i.e. sudo ALIASNAME) will not work because aliases are specific to your user's Bash session.

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.