I have a simple script I am building that monitors the auth.log for su/ssh sessions being opened. If it sees a session opened using su or ssh it sends a push message using Gotify. I have tested the Gotify command by putting it in its own script and running it and it works but as soon as it is in the while loop it just hangs. I have found a fix from this question Shell script hangs on mail command by redirecting the input from /dev/null to the gotify command, but I am unsure why I need to do it in this particular instance and not when I use it in other scripts (I have a few other scripts where i use gotify without the redirect).
The script is:
#! /bin/bash
pipe=$(mktemp -u)
mkfifo "${pipe}"
log="/var/log/auth.log"
trap "rm -f $pipe" EXIT
tail -f $log >> $pipe &
while read line;
do
aryLine=(${line})
if [[ "${aryLine[@]:5:3}" =~ "pam_unix("(su|sshd)":session): session opened" ]]; then
echo "session opened for ${aryLine[@]:10:4}"
/usr/bin/gotify push -p 3 --title "login" "login from ${aryLine[10]}" < /dev/null
fi
done < $pipe
I am at a loss why I need the /dev/null to stop the script hanging when using gotify within this particular while/if loop and any help understanding will be appreciated. The code below works and spams my gotify just fine:
#! /bin/bash
while true; do
if true; then
/usr/bin/gotify push -p 3 --title "login" "login from ${aryLine[10]}"
fi
done
I am using Debian Bullseye.