Ilija Matoski

    ← Articles

    Confirm before executing shutdown/reboot command on linux

    I was rushing to leave and was still logged into a server so I wanted to shutdown my laptop, but what I didn’t notice is that I was still connected to the remote server. Luckily before pressing enter I noticed I’m not on my machine but on a remote server. So I was thinking there should be a very easy way to prevent it from happening again, to me or to anyone else.

    So first thing we need to create a new bash script at /usr/local/bin/confirm with the contents bellow and with execution permissions

    #!/usr/bin/env bash
    echo "About to execute $1 command"
    echo -n "Would you like to proceed y/n? "
    read reply
    if [ "$reply" = y -o "$reply" = Y ]
    then
    $1 "${@:2}"
    else
    echo "$1 ${@:2} cancelled"
    fi

    Now only thing left to do is to setup the aliases so they go through this command to confirm instead of directly calling the command.

    So I create the following files

    /etc/profile.d/confirm-shutdown.sh

    alias shutdown="/usr/local/bin/confirm /sbin/shutdown"

    /etc/profile.d/confirm-reboot.sh

    alias reboot="/usr/local/bin/confirm /sbin/reboot"

    Now when I actually try to do a shutdown/reboot it will prompt me like so.

    ilijamt@x1 ~ $ reboot
    Before proceeding to perform /sbin/reboot, please ensure you have approval to perform this task
    Would you like to proceed y/n? n
    /sbin/reboot cancelled