I’ve composed a bash script that uses SSMPT to email me when a user access port 25565 (minecraft server). Here’s the .sh
#!/bin/bash
log_file="/var/log/minecraft-connections.log"
email_recipient="recipient@example.com"
smtp_host="smtp.example.com"
smtp_port="587"
email_username="your-email@example.com"
email_password="your-email-password"
# Continuously monitor port 25565 and log incoming connections
nc -klv localhost 25565 >> "$log_file" &
# Monitor the log file for changes and send email notifications
tail -fn0 "$log_file" | while read -r line
do
echo "$line" | grep -q "localhost\[127.0.0.1\]:.*ESTABLISHED" && {
echo "Incoming connection detected. Sending email notification..."
echo -e "Subject: Minecraft Connection\n\n$line" | ssmtp -au "$email_username" -ap "$email_password" -h "$smtp_host" -p "$smtp_port" "$email_recipient"
echo "Email notification sent."
}
done
I’ve been having trouble getting it to run as a service - is there a .service
template I can follow that works with Dietpi-Service?
Cheers as always.
Jappe
June 10, 2023, 1:19pm
2
You would need to create a systemd service and then add it to dietpi services list: dietpi-services
> Add
.
Simple tutorial for creating a service
systemd.service manpage
Hi Jappe,
Thanks for taking time to get back to me. I should add, I have made the bash script executable.
I wrote this, very basic minecraft-port-log01.service
[Unit]
Description= Minecraft 25565 port logging
[Service]
ExecStart=/home/dietpi/minecraft-port-logging-01.sh
[Install]
WantedBy=multi-user.target
Which always fails. Here’s the log:
│
│ ● minecraft-port-log01.service - Minecraft 25565 port logging │
│ Loaded: loaded (/etc/systemd/system/minecraft-port-log01.service; enabled; vendor │
│ preset: enabled) │
│ Active: failed (Result: exit-code) since Sat 2023-06-10 14:42:56 BST; 5s ago │
│ Process: 3357 ExecStart=/home/dietpi/minecraft-port-logging-01.sh (code=exited, │
│ status=203/EXEC) │
│ Main PID: 3357 (code=exited, status=203/EXEC) │
│ CPU: 756us │
│ │
│ Jun 10 14:42:56 MMServer03 systemd[1]: Started Minecraft 25565 port logging. │
│ Jun 10 14:42:56 MMServer03 systemd[3357]: minecraft-port-log01.service: Failed to │
│ execute /home/dietpi/minecraft-port-logging-01.sh: No such file or directory │
│ Jun 10 14:42:56 MMServer03 systemd[3357]: minecraft-port-log01.service: Failed at step │
│ EXEC spawning /home/dietpi/minecraft-port-logging-01.sh: No such file or directory │
│ Jun 10 14:42:56 MMServer03 systemd[1]: minecraft-port-log01.service: Main process │
│ exited, code=exited, status=203/EXEC │
│ Jun 10 14:42:56 MMServer03 systemd[1]: minecraft-port-log01.service: Failed with result │
│ 'exit-code'.
The directory certainly exists, it’s the default dietpi directory.
you could try to add your bash code to our autostart script. Simply run dietpi-autostart
> option 14 or 17.
1 Like
Ah, thanks @Joulinar - I haven’t used that functionality yet. I’ll give it a try.
EDIT:
That seems to be working. Result of journalctl -u dietpi-autostart_custom
:
-- Journal begins at Sat 2023-06-10 16:10:16 BST, ends at Sat 2023-06-10 16:10:49 BST. --
Jun 10 16:10:17 MMServer03 systemd[1]: Started DietPi-AutoStart custom script.
Jun 10 16:10:17 MMServer03 custom.sh[642]: Listening on localhost 25565
Thanks very much for the pointer. So I assume the bash script is working - I wonder why the .service
file couldn’t locate the bash script in the dietpi home folder.