We can do this easily using curl, Gotify and a crontab one-liner (cronjob)! I’ll show you how!
This assumes you already have Gotify installed and set up, you can get the server and app for free on gotify.net (yes it’s self-hosted)!
Sign in to your Gotify server’s dashboard, and then click on the “APPS” tab where you will click the button “CREATE APPLICATION”, where you will enter a title and short description, for example “Home Server” and “Notifications about Home Server Events” or anything you fancy and click “CREATE”.
You will now see the new entry you just created, with a column called “TOKEN”, hidden for security reasons. Click the eye icon to reveal the token and copy it. We will need it now on the server you want to be notified about on startup.
On your server you want to monitor startups on, sign in as root (or user with sudo privilege) and install curl if it isn’t already installed.
Now that curl is installed sign in as a user with the privilege to run curl and privilege to connect to the internet, and create a new cronjob with:
If asked, choose the editor nano (input the number and hit enter), and the editor should now open the crontab for the user you’re signed in as.
Go to the end of the file and copy-paste the following line:
@reboot /usr/bin/curl "https://gotify.example.com/message?token=TOKEN" -F "title=Server started" -F "message=Server just booted up" -F "priority=5"
TOKEN with the token you got from the Gotify dashboard earlier and replace
gotify.example.com with the actual Gotify server address of the server you are using. You can customize the title and notification text and priority to your liking. Once happy, press CTRL + X, confirm you want to save your changes with Y and hit enter twice to save and exit.
@reboot means this job will be executed on startup. So the next time the server starts up, it will use curl to send a notification to your Gotify server, which in turn will push it to your Android device where the Gotify app is installed and set up (Make sure to sign in to the app as the same user as the one you signed in to the dashboard to create the app notification token).
Now try rebooting the server, and you should get a notification when the server is back up.
That’s all there is to it!
Why would I want to know when my server started up?
This can be useful when your server was not supposed to restart (server fault, power out or similar) so that you can investigate. It’s also useful in cases where your server is fully encrypted, and it starts up just enough so that you can SSH into it and unlock the drives. After a fault and reboot, you will want to know that it’s back up, either to unlock the server or to investigate what happened. Otherwise, you might never know something caused a reboot/that it was down!
Make sure the server you are monitoring and the server Gotify is running on, is not the same, or alternatively add
/bin/sleep 120; in front of the curl command to give Gotify enough time to start up and listen to notifications. If that’s not enough, increase the time (it’s seconds).
Make sure the user you created the cronjob as, has permission to run curl and permission to connect to the internet.
Make sure your server can reach the Gotify server (check if DNS resolution works, try pinging 22.214.171.124 to see if you can access the internet).
Make sure curl can handle
https:// if your server is using HTTPS (highly recommended that it does).