The main part of my home office network includes a media server, a torrent server, and my desktop PC. Both servers are headless and I have automated some functionality on them. I got tired of tediously logging into the servers so I decided to have them notify my when a scheduled task is complete.
For the media server, I want to know when the scheduled daily backups are done. The backups happen via a custom script I wrote that runs daily via a cronjob. For this one, I used "sshpass" in the script to send a "notify-send" desktop notification to my office PC. It looks like this:
. The hidden file "~/.passfile" contains my desktop password so it will allow the office PC to receive the message. This was pretty easy.
On the torrent server, I have it do a morning reboot because it's on a VPN and the external address changes once in a while, which then blocks the torrent server. Rather than checking it every day to see if it's still up, I just reboot it nightly via a cronjob. It's located in a place I can't see it, so a visual check to verify it was up and running was problematic so a notification was needed. However, getting a notification from the pi was a lot trickier. The problem I had was timing; getting the message sent when reboot was complete, but also when the network was up. Obviously, you can't send a message over the network until the network is connected.
I tried all the things the internet suggested: a bashrc entry, an rc.local entry, an entry in ifup, - none worked. Some suggested adding a "wait" command but for how long? Would a few seconds be enough or a couple minutes? Seemed like a hacky solution.
Systemd was the answer. Turns out you can rather easily create and activate a systemd service. Here's what I did:
Created an executable bash command file with basically the same "sshpass..." content as the server has with a different message and saved it in the home directory on the pi.
Created a custom service with this command: sudo systemctl edit --force --full bootupnotice.service
Entered this in the service file:
Verified it with: sudo systemctl --status bootupnotice.service
Enabled it: sudo systemctl enable bootupnotice.service
Started it: sudo systemctl start bootupnotice.service
And it works! The "Wants..." and "After..." parts of the systemd entry do exactly what I wanted - wait for the network to be running and connected.
The only thing I'm not totally happy about is my login password in plain text to be able to use sshpass. It is in a hidden file, but that's not much security. I'm looking into how to encrypt it.
GPG encrypted the password file. Done.
For the media server, I want to know when the scheduled daily backups are done. The backups happen via a custom script I wrote that runs daily via a cronjob. For this one, I used "sshpass" in the script to send a "notify-send" desktop notification to my office PC. It looks like this:
Code:
sshpass -f ~/.passfile ssh office -p 7777 "DISPLAY=:0 notify-send -i package-install -t 0 'Server backup:' 'Media backups complete.'"
On the torrent server, I have it do a morning reboot because it's on a VPN and the external address changes once in a while, which then blocks the torrent server. Rather than checking it every day to see if it's still up, I just reboot it nightly via a cronjob. It's located in a place I can't see it, so a visual check to verify it was up and running was problematic so a notification was needed. However, getting a notification from the pi was a lot trickier. The problem I had was timing; getting the message sent when reboot was complete, but also when the network was up. Obviously, you can't send a message over the network until the network is connected.
I tried all the things the internet suggested: a bashrc entry, an rc.local entry, an entry in ifup, - none worked. Some suggested adding a "wait" command but for how long? Would a few seconds be enough or a couple minutes? Seemed like a hacky solution.
Systemd was the answer. Turns out you can rather easily create and activate a systemd service. Here's what I did:
Created an executable bash command file with basically the same "sshpass..." content as the server has with a different message and saved it in the home directory on the pi.
Created a custom service with this command: sudo systemctl edit --force --full bootupnotice.service
Entered this in the service file:
Code:
[Unit] Description=Boot up notification Wants=network-online.target After=network-online.target [Service] Type=simple User=pi WorkingDirectory=/home/pi ExecStart=/home/pi/bootupnotice [Install] WantedBy=multi-user.target
Verified it with: sudo systemctl --status bootupnotice.service
Enabled it: sudo systemctl enable bootupnotice.service
Started it: sudo systemctl start bootupnotice.service
And it works! The "Wants..." and "After..." parts of the systemd entry do exactly what I wanted - wait for the network to be running and connected.
GPG encrypted the password file. Done.



