This script, named tailscript.sh, tails a .out file looking for an occurrence of the string 'SocketException' and then executes another script, named lsof.sh.
#!/bin/bash
Run the script in the background
nohup bash -c '
Tail the log file and search for the string "SocketException"
tail -f $MY_DIR/blah/logs/errors.out | while read line; do
if [[ "$line" == "SocketException" ]]; then
# If the string is found, execute the script "/home/user/scripts/lsof.sh"
bash lsof.sh
fi
done
' &
The lsof.sh script should output to a file called results.log:
#! /bin/bash
pid=$(ps -ef | grep my_proc | awk '{print $2}')
lsof -n -p $pid > results.log
If I do:
echo "SocketException" >> $MY_DIR/blah/logs/errors.out
It works fine and the expected results.log is created.
The actual SocketException error only occurs maybe once every 3 weeks and each time it happens, no results.log is generated. I verify the processes are still running and that the specified string, 'SocketException' exists in the file. I then issue the echo command to manually insert the string into the log, but still no results.log is generated as it was the day I kicked off the script. Could something be causing the tailscript.sh to hang up or just stop working after a period of time? Anything else to look for or a better way to accomplish this?
tail -F
instead of lowercase-f
to cope with this scenario. – tripleee Jan 09 '23 at 16:49Edit: Disregard, a quick Google answered my question :) Will give this a try.
– nettwerker1 Jan 09 '23 at 17:02tail -f
simply opens the file once. Withtail -F
you telltail
to try again if the file disappears from underneath you. – tripleee Jan 09 '23 at 17:03