0

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?

  • Does the log file get rotated from time to time? Try with tail -F instead of lowercase -f to cope with this scenario. – tripleee Jan 09 '23 at 16:49
  • The log file does get rotated when it reaches a certain size. What is the difference? 'Man tail' says "-F same as --follow=name --retry" but I don't quite grasp what this means.

    Edit: Disregard, a quick Google answered my question :) Will give this a try.

    – nettwerker1 Jan 09 '23 at 17:02
  • tail -f simply opens the file once. With tail -F you tell tail to try again if the file disappears from underneath you. – tripleee Jan 09 '23 at 17:03

0 Answers0