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?
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?
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 asreboot
:Create a file with this content and save it as
/usr/bin/reboot
:Make it executable:
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 runreboot
.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
What's also cool is that you can press Ctrl+C to cancel out of this function
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:
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:
Now we need to alias this long command to
reboot
, so that it will be run instead of the original/sbin/reboot
one: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 withsudo
, you must add thesudo
inside the alias command:Running the alias itself with
sudo
(i.e.sudo ALIASNAME
) will not work because aliases are specific to your user's Bash session.