Skip to content

How to Recieve Notifications When Your Server Finishes Booting Up [HOWTO]

May 23, 2010

One of the more annoying things about performing maintenance on a server through ssh is finding out when the server is up again after a reboot. I’m constantly trying to log into the server over ssh and checking to see if it’s up. Here’s a novel idea, why don’t I figure out how to get the server to notify me when it’s up! While reading about make server beep after bootup and customizing Notify OSD notification, I got the idea to have the server send me an OSD notification using the rc.local script.

Notify OSD notifications are the small bubbles that popup in the upper right hand corner of the Ubuntu screen, just underneath the panel noticiations area. The most common OSD notification is probably the network connection notification as seen in the screenshot below.

rc.local is a file that the server runs once the server is fully booted.

The method to which the server notifies the desktop client is as follows:

How to Setup Server Up Notifications

Nomenclature:

  • Server – the server machine that reboots and sends to the OSD notificaton.
  • Client – the desktop machine that receives the OSD notificaton.
  • <client_user_name> – the username of the client computer that recieves the OSD notifications.
  • <server_user_name> – the username of the server computer.
  • <client_computer_name> – the name of the client computer.
  • <server_computer_name> – the name of the server computer.
  • <domain_name> – the name of the network domain.

Client Setup

I’m using Ubuntu Lucid 10.04 as my desktop OS.

1. Install OpenSSH server.

sudo apt-get -y --force-yes install openssh-server

Server Setup

I’m using Ubuntu Server Karmic 9.10 as my server OS.

1. Install Notify OSD.

sudo apt-get -y --force-yes install notify-osd

2. Test to see if the client will recieve the OSD notification.

ssh -X <client_user_name>@<client_computer_name>.<domain_name> 'DISPLAY=:0 notify-send "Server is up" -i "/usr/share/icons/Humanity/apps/48/bash.svg"'

Example:

voltron43@server: ~$ ssh -X voltron43@voltron43-laptop.local 'DISPLAY=:0 notify-send "Server is up" -i "/usr/share/icons/Humanity/apps/48/bash.svg"'

where

  • “Server is up” is the notification message
  • -i “/usr/share/icons/Humanity/apps/48/bash.svg” is the location of the icon on the client computer shown in the OSD notification.

Once the test is successful, we need to move onto password-less SSH logins in order for the server to send the OSD notification without having to prompt for the client username’s password. Because rc.local uses root instead of the Server superuser, we need to setup the password-less SSH login with root.

3. On the Server, switch to root user.

sudo su

4. Generate RSA keys.

ssh-keygen -t rsa

5. Accept the default file and leave the passphrase blank.

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.

6. Copy the RSA key to the client computer and client username.

ssh-copy-id -i /root/.ssh/id_rsa.pub <client_user_name>@<client_computer_name>.<domain_name>

Example:

ssh-copy-id -i /root/.ssh/id_rsa.pub voltron43@voltron43-laptop.local

7. Test the OSD notification from root to make sure there isn’t a password prompt.

ssh -X <client_user_name>@<client_computer_name>.<domain_name> 'DISPLAY=:0 notify-send "Server is up" -i "/usr/share/icons/Humanity/apps/48/bash.svg"'

Example:

root@server: ~$ ssh -X voltron43@voltron43-laptop.local 'DISPLAY=:0 notify-send "Server is up" -i "/usr/share/icons/Humanity/apps/48/bash.svg"'

8. Create the rc.local file

sudo nano /etc/init.d/local

The Ubuntu equivalent to rc.local is the local file located in the init.d folder.

9. Paste the following into the local file.

#!/bin/sh
ssh -X @. 'DISPLAY=:0 notify-send "Server is up" -i "/usr/share/icons/Humanity/apps/48/bash.svg"'
exit 0

10. Save and exit nano by pressing Ctrl+O and Ctrl+X.

11. Make the local file executable.

sudo chmod +x /etc/init.d/local

12. Link the local file with Init.

sudo update-rc.d local defaults 80

13. Restart the server and you’re done!

sudo reboot

Debugging

To debug the script, add “> /home/<server_user_name>/error.log 2>&1″ to the local file at the end of the ssh command to check for errors and “echo $(date) – Running >> /home/<server_user_name>/test.run” in the next line to see if the local file is being executed on startup.

#!/bin/sh
ssh -X <client_user_name>@<client_computer_name>.<domain_name> 'DISPLAY=:0 notify-send "Server is up" -i "/usr/share/icons/Humanity/apps/48/bash.svg"' > /home/<server_user_name>/error.log 2>&1
echo $(date) - Running >> /home/<server_user_name>/test.run
exit 0

Example:

#!/bin/sh
ssh -X ssh -X voltron43@voltron43-laptop.local 'DISPLAY=:0 notify-send "Server is up" -i "/usr/share/icons/Humanity/apps/48/bash.svg"' > /home/voltron43/error.log 2>&1
echo $(date) - Running >> /home/voltron43/test.run
exit

References

Advertisement

From → Uncategorized

One Comment
  1. l e . n o x permalink

    Hi,

    On the last “Example:” you have double ssh -X ;O)

    #!/bin/sh
    ssh -X ssh -X voltron43@voltron43-laptop.local ‘DISPLAY=:0 notify-send “Server is up” -i “/usr/share/icons/Humanity/apps/48/bash.svg”‘ > /home/voltron43/error.log 2>&1
    echo $(date) – Running >> /home/voltron43/test.run
    exit

    Very nice blog, I learne a lot of think

    Have good day, Voltron43

    L e . NoX

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.