Skip to content

Notify on boot

Sometimes its handy to send a notification on boot, for example to send the IP address that the server has.

This script uses discord to send a message.

  1. Install dependencies

    sudo apt install -y jq
    
  2. Install discord.sh

    cd /usr/local/bin
    sudo curl -L -O https://github.com/fieu/discord.sh/releases/download/v2.0.0/discord.sh
    sudo chmod +x discord.sh
    
  3. Create a boot-notify.sh script to send a message

    /opt/boot-notify.sh
    IP=$(hostname -I | awk '{print $1}')
    WEBHOOK_URL=<https://discord.com/api/webhooks/<webhook-url>>
    
    discord.sh \
    --webhook-url="$WEBHOOK_URL" \
    --username="server" \
    --text="server booted with IP $IP"
    
    sudo chmod u+x /opt/boot-notify.sh
    
  4. Create a boot-notify systemd task that runs after networking has started

    /etc/systemd/system/boot-notify.service
    [Unit]
    Description=Sends a message over discord at startup
    After=network-online.target
    
    [Service]
    Type=simple
    ExecStart=/bin/bash /opt/boot-notify.sh
    
    [Install]
    WantedBy=multi-user.target
    
  5. Test the systemd service

    sudo systemctl daemon-reload
    sudo systemctl start boot-notify
    

    Then verify the message is received

  6. Enable the systemd service

    sudo systemctl enable boot-notify
    

Last update: August 12, 2023
Created: May 27, 2023