Create a Systemd Service to Send Automatic Emails When Arch Linux Restarts

Getting around with systemd, we’ll create a service to send emails every time ArchLinux VM restarts. As a bonus, email message will be configured to show a public IP address of the box.

Environment

We have an archbox as below:

$ uname -rv
3.10.29-2-ARCH #1 PREEMPT Mon Feb 10 04:04:41 MST 2014

With SSMTP and mail applications:

$ ssmtp -V ; mail -V
sSMTP 2.64 (Not sendmail at all)
v14.5.2

SSMTP configuration is beyond the scope of this article. If you need any help setting it up, please check Logwatch, SSMTP and Iptables or Arch Linux (Raspberry Pi) post.

Configuration

Create a new directory to store script files:

# mkdir /root/.myscripts

Open a new file for the script:

# vim /root/.myscripts/email-on-reboot.sh

And add the following code:

#!/bin/bash
sleep 20
IP=$(curl -s https://ip.lisenet.com)
sleep 5
echo "My public IP is: ${IP}"|mail -S sendwait -s "RESTARTED: Back online" -r [email protected] [email protected]
exit

Create email-on-reboot named systemd service:

# touch /etc/systemd/system/email-on-reboot.service

Set permissions on the file:

# chmod 0644 /etc/systemd/system/email-on-reboot.service

Open for editing:

# vim /etc/systemd/system/email-on-reboot.service

And add the following lines:

[Unit]
Description=Email After Reboot
DefaultDependencies=no
[email protected]

[Service]
Type=oneshot
ExecStart=/bin/bash /root/.myscripts/email-on-reboot.sh

[Install]
WantedBy=multi-user.target

Enable newly created email-on-reboot service:

# systemctl enable email-on-reboot.service
ln -s '/etc/systemd/system/email-on-reboot.service' '/etc/systemd/system/multi-user.target.wants/email-on-reboot.service'

Start the email-on-reboot service:

# systemctl start email-on-reboot.service

Check status of the service:

# systemctl status email-on-reboot.service -l
email-on-reboot.service - Email After Reboot
 Loaded: loaded (/etc/systemd/system/email-on-reboot.service; enabled)
 Active: inactive (dead) since Thu 2014-02-13 18:47:51 GMT; 48s ago
 Process: 29371 ExecStart=/bin/bash /root/.myscripts/email-on-reboot.sh (code=exited, status=0/SUCCESS)
 Main PID: 29371 (code=exited, status=0/SUCCESS)

Feb 13 18:47:47 archbox sSMTP[29373]: Creating SSL connection to host
Feb 13 18:47:47 archbox sSMTP[29373]: SSL connection using AES256-GCM-SHA384
Feb 13 18:47:48 archbox sSMTP[29373]: Sent mail for [email protected] (221 mail.example.com closing connection) uid=0 username=root outbytes=531
Feb 13 18:47:51 archbox systemd[1]: Started Email After Reboot.

4 thoughts on “Create a Systemd Service to Send Automatic Emails When Arch Linux Restarts

  1. Systemd seems to be quite complicated to get around with due to lack of usable examples. Thanks a lot man!

  2. Instead of sleeping after mail, pass this arg to mail

    -S sendwait

    to get it to wait until the async sub process (sendmail) has finished before exiting

  3. “And make sure it’s executable:”

    Actually you want to make sure it’s NOT executable in Ubuntu 18.04 or it will complain incessantly that the execute bit is set and should be disabled.

Leave a Reply to Steven Barre Cancel reply

Your email address will not be published. Required fields are marked *