Tuesday, March 6, 2012

halt or Halt?

Some folks missed the old halt command which seems to work the same as power off. OpenSuSE now uses /sbin/shutdown for that matter, how ever you can still have the same command with some few steps as explained below.

Create  the file    /etc/bash/bashrc.local    (assuming that you are using bash as your shell ) or any place that a shell is sourced when it is invoked and put the line below.

  Halt() { if (( $EUID == 0 )); then  shutdown -h -P now; else su root -c'shutdown -h -P now'; fi ; }  



Now as a normal user and root can invoke the command   Halt    and will point to /sbin/shutdown with the arguments -h -P. As a normal user you will be ask for the root's password. Notice the capital H and not h, halt is already in the root's path so we used the capital H but if you want to use halt to replace Halt from the beginning of the line which is still possible and without any issues, the only issue with this is that you get used to that command that is defined in your current system and then when your not in that system and run halt, you will end up scratching your head as to why it did not work as expected! Now how can you be so sure that this is the first to be called and not the one that is in your root's path? See man bash COMMAND EXECUTION for more detailed info. So how can you be so sure that bash is your default shell? you can check the config file  /etc/passwd   by running the command.  grep username /etc/passwd    where username is your username.

To explain about whats going on in that line above here are some explanations.

That line is called a function see man bash and look for functions. In short functions are the same as an alias but more powerful and has more advantages. It is using the if clause/statement that checks if the user's user id that is running that command is equal to 0 (which is root) or not. When it found that root is running the command it will run the first command which is  shutdown -h -P    if it is not root it will run the second  command which is   su root -c'shutdown -h -P now'  . As to why we choose to put  in   /etc/bash/bashrc.local   run   head -n 8 /etc/bash.bashrc   so you can read up about that local file. Come 12.1 systemd is in used by default so for those of you who are a systemd hardcore fan boys/girls you can just replace the  shutdown -h -P now  with   systemctl poweroff   and your systemd will be in full force :-). This setup is not specific to any DE, it will work even on a minimal install or the server install. KDE has its own settings which you can find in    /usr/share/kde4/config/kdm/kdmrc     other DE might have their own config file as well. When you want to check if a program or command  is already installed one would use the external command  which  but it does not know about functions or builtins of bash. One could use the built in   type   of bash as a normal user run  type Halt  and you should see something like this.

Halt is a function
Halt ()
{
    if (( $EUID == 0 )); then
        shutdown -h -P now;
    else
        su root -c'shutdown -h -p now';
    fi
}

Add an -a flag so you can see if there are more than one match. A Reboot function is easily created using the same syntax by the way :-).

Enjoy folks!


No comments:

Post a Comment