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.
- Create a vanilla Ubuntu 20.04 server template
- Run a server using that template
- Configure the mail settings on the server
- Create an email sending script
- Configure the template to send email notifications when the server boots and shuts down
- Test the notifications
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
- Go to Blueprint -> Templates.
- 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.
Run a server using that template
- In the template table in the Blueprint -> Templates section, click on the name of the template you have just created.
- Go to the Servers tab.
- Press the "Add server" button and then select "Add server".
- 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.
- Press the "Add server" button to create the server.
- Wait for your server status to become inactive and then press on the "Boot" action for the server.
Configure the mail settings on the server
- 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.
- Run the following command to install an email sending command:
apt-get install -y 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.
- 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 replaceusername@gmail.com
andpassword
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
- Go to Blueprint -> Scripts.
- 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.
- Click on the name of the script you have just created.
- Press the "Edit" button to modify the code of the script.
- Add three parameters for the script: RECEIVER, SUBJECT and BODY.
- 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.
Configure the template to send email notifications when the server boots and shuts down
- 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.
- 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.
- 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.
- 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.
- 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.
Test the notifications
We are going to test the configured email notifications by shutting down and booting the server:
- Go to Compute -> Servers and click on the name of the server, and then go to the Events tab.
- Press the "More actions" button and select Shutdown.
- 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.
- Once the server is inactive, press the "More actions" button and select Boot.
- 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.