0

I'm trying to make a script that can tail log files from a remote server to my local directory. tail -F is what I'm using but after piping it with gzip, nothing happens although a local copy of the log file is created.

Update: The script runs but it can't reach the gzip command since I have to type ctrl+c to end the tailing. Thus it ends the script without even zipping it.

to_Tomcat(){
        # tail log file -> zips it using gzip 
        tail -F /sampleRemoteDirectory/logs/tomcat/sample.log > "$TomcatLogFileName"-Tomcat.log | gzip "$TomcatLogFileName"-Tomcat.log
        echo ""
        echo "...tailing the log file and saving it as $TomcatLogFileName-JBoss.log.gz"
        echo ""
    }

to_Tomcat TomcatLogFileName
                sleep 10            
ret=$?

# last note before the user has to exit the shell script

echo ""
echo "Saved file: $TomcatLogFileName-Tomcat.log.gz"
  • 2
    You're piping to gzip, which you shouldn't be doing. Change the | to ;. – forest May 14 '19 at 1:37
  • Thank you, I'll update the script and run it using your suggestion. – Momonga May 14 '19 at 1:38
  • The script runs but it can't reach the gzip command since I have to type ctrl+c to end the tailing. Thus it ends the script without even zipping it. – Momonga May 14 '19 at 2:35
  • 1
    If you want to take a snapshot of the end of the log file, try something like timeout 60s tail -F /sampleRemoteDirectory/logs/tomcat/sample.log, which will run tail for a minute. – Mark Plotnick May 14 '19 at 3:08
  • Noted @Mark, I will add updates once the suggestion satisfies the query :) – Momonga May 14 '19 at 3:15
2

tail -f is meant to be interactive, other than timeout you should try tail -100 (100 or whatever) to catch last lines.

main part would be

tail -100 /whatever/sample.log | gzip > /whatever/sample.log.gz
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.