How to send start and stop notifications with boot and shutdown scripts

Boot and shutdown scripts are a powerful tool that allows you to have tasks run when a server starts and/or stop. In this how to we configured a pair of boot and shutdown scripts so that a server sends an email notification when it starts and when it stops.

Prerequisites

  • An email account, in this example we will use a Gmail one with 2-factor authentication disabled but the mail settings could be adapted to other email providers.
  • A cloud account on which to deploy the server

Create a vanilla Ubuntu 20.04 server template

  1. Go to Blueprint -> Templates. Blueprint templates
  2. Press the "Add template" button. Fill in the form with a name for your template, select Ubuntu 20.04 Focal Fossa x86_64 as Generic Image and press the "Add template" button to create the template. New template

Run a server using that template

  1. In the template table in the Blueprint -> Templates section, click on the name of the template you have just created. Templates table
  2. Go to the Servers tab. Template servers
  3. Press the "Add server" button and then select "Add server". Add template server
  4. Fill in the opening form:
    • Type in a name for your server,
    • Do not select a VPC,
    • Select a cloud account and the zone where you want to deploy the server,
    • Select a server plan,
    • Select the Default firewall as Firewall Profile. New server form
  5. Press the "Add server" button to create the server.
  6. Wait for your server status to become inactive and then press on the "Boot" action for the server. Boot template server

Configure the mail settings on the server

  1. Wait until your server is operational and then access it as root through SSH as described in the How to access your server how-to.
  2. Run the following command to install an email sending command: apt-get install -y ssmtp Installing SSMTP > The use of a cookbook would be a more adequate way to perform this step, though for the sake of brevity in this example we are running it manually. See this how-to for more information on how to use cookbooks.
  3. Configure the SMTP service to allow sending emails using your email account. You can do this by editing the contents of the /etc/ssmtp/ssmtp.conf file. The following command can be used to configure a Gmail account, without 2-factor authentication enabled, provided you replace username@gmail.com and password with your email address and password respectively (you will also have to enable the Allow less secure apps option in your Google account):
cat <<EOC >/etc/ssmtp/ssmtp.conf
UseSTARTTLS=YES
FromLineOverride=YES
root=username@gmail.com
mailhub=smtp.gmail.com:587
AuthUser=username@gmail.com
AuthPass=password
EOC

Create an email sending script

  1. Go to Blueprint -> Scripts. Blueprint scripts
  2. Press the "Add script" button. Fill in the form with a name and a description for your script and press the "Add script" button to create the script. New script
  3. Click on the name of the script you have just created. Scripts table
  4. Press the "Edit" button to modify the code of the script. Script code
  5. Add three parameters for the script: RECEIVER, SUBJECT and BODY. Adding script parameters
  6. The code below sends an email to the RECEIVER with the SUBJECT and BODY.
#!/bin/bash
cat <<EOM | sendmail ${RECEIVER}
Subject: ${SUBJECT}

 ${BODY}
EOM

Replace the code of the script with it and press the "Update code" button. Editing the code of the script

Configure the template to send email notifications when the server boots and shuts down

  1. Go to Blueprint -> Templates and in the template table there, click on the name of your server's template. Then go to the Scripts tab. Scripts of a template
  2. In order to have the server send an email when it boots, we are going to configure a boot script. Click on "Add script" and select the script you have just created. Adding a boot script
  3. Fill in the opening form with the email address that will receive the notifications as RECEIVER, a subject for the notification email as SUBJECT and a body for the email as BODY. Then press the "Add script" button. Configuring a boot script
  4. Now, in order to have the server send an email when it shuts down, we are going to configure a shutdown script. Click on "Add script" and select the script you have just created. Adding a shutdown script
  5. Fill in the opening form with the email address that will receive the notifications as RECEIVER, a subject for the notification email as SUBJECT and a body for the email as BODY. Then press the "Add script" button. Configuring a shutdown script

Test the notifications

We are going to test the configured email notifications by shutting down and booting the server:

  1. Go to Compute -> Servers and click on the name of the server, and then go to the Events tab. Server events
  2. Press the "More actions" button and select Shutdown. Shutting server down
  3. The server starts to shut down but will run our shutdown script before doing so. There should be an email notifying the server's shutdown in the receiver's address inbox. Shutdown script has run
  4. Once the server is inactive, press the "More actions" button and select Boot. Booting server
  5. The server boots and will run our shutdown script before becoming operational. There should be an email notifying the server's boot in the receiver's address inbox. Boot script has run