This will allow you to sent an email at startup on Ubuntu boot. You will need to ensure sendmail is setup and working (read my guide on How to send email via G Suite from Ubuntu in the cloud, setup an Ubuntu server in the cloud here (guide here)).
Create a scripts folder
Create a file called /scripts/emailstartup.sh and add..
#!/bin/bash echo "Dumping startup log"; journalctl -b0 --system _COMM=systemd --no-pager >/scripts/boot.log #echo "Deleting old log file"; #rm -R /scripts/boot.zip #echo "Zipping up Startup Log File."; #zip -r -9 /scripts/boot.zip /scripts/boot.log echo "Sending Email With Attachment"; # Ensure you have gmail or gsuite setup on your domain, guide here https://fearby.com/article/moving-a-cpanel-domain-with-email-to-a-self-managed-vps-and-gmail/ sendemail -f [email protected] -t [email protected] -u "Startup: $HOSTNAME server" -m "Attached are the startup logs for $HOSTNAME server" -s smtp.gmail.com:587 -o tls=yes -xu [email protected] -xp password -a /scripts/boot.log
optional: Uncomment lines above to attach a zip file instead of a log file (don’t forget to attach the zip instead of the log file in sendmail.)
Make the script file executable
sudo chmod +X /scripts/emailstartup.sh
Test the script
sudo /bin/bash /scripts/emailstartup.sh Dumping startup log Deleting old log file Zipping up Startup Log File. adding: scripts/boot.log (deflated 91%) Sending Email With Attachment Sep 05 18:49:27 yourservernamehere sendemail: Email was sent successfully!
Add the following to crontab -e to ensure the script is executed 5 minutes after startup.
@reboot sleep 300 && /bin/bash /scripts/emailstartup.sh >> /dev/null 2>&1
On reboot, you will be emailed desired start-up information.
Donate and make this blog better
Ask a question or recommend an article
[contact-form-7 404 "Not Found"]
v1.0 initial post