Currently, the only way to see if my Tomcat has fully started up is to check apache-tomcat log:

tail -f ./catalina.out

and look for this pattern to show up before I do any job.

org.apache.catalina.startup.Catalina.start Server startup in [0-9]* ms

I wanted to automate the process and wrote the script below, which did work.

tail -f ./catalina.out | egrep -q '*.Catalina.start Server startup in [0-9]* ms$' 
touch done

But the problem is that it sometimes reads the previous Startup and finishes right off.

The desired process:

Tomcat stops -> Tomcat restarts -> The tail shell runs -> Tomcat full starts up -> The shell tells me it's done

What my script might do:

Tomcat stops -> Tomcat restarts -> The tail shell runs -> It immediately tells me Tomcat is fully booted because of the previous log.

How do I achieve this?

  • Maybe instead of checking the logfile you could check if Tomcat's URL is reachable. Or to stay with your current approach you could delete or rename the logfile in a script you use to start Tomcat.
    – Bodo
    Apr 19, 2021 at 10:59
  • 1
    This is a suboptimal apporoach. You suffer delays while the outgoing log file is buffered, while the filesystem updates, while tail buffers output, while grep (without --line-buffered) waits for a buffer full of lines. Use tools like pgrep and lsof to catch the startup.
    – waltinator
    Apr 19, 2021 at 17:36

1 Answer 1


By default tail -f reads the 10 last lines, and then starts with following up. You can use -n to override that number, so tail -n0 -f starts at the current end, ignoring any earlier content, and then follows new changes.

Note that if you run:

start tomcat in some way
tail -n0 -f logfile | grep -q

you're relying on tomcat being slow enough that the tail has time to start before tomcat has written the line telling it started. Usually, it might be so, but it's possible for the script to be scheduled so that there's an arbitrarily-long delay before tail runs. So, really, you should somehow arrange for tail to start first.

I could imagine something like this, but I'm not sure:

tail -n0 -f logfile | grep -q &
start tomcat in some way
wait "$pid"

Also, there's a slight problem in that while grep -q exits the moment it sees a matching line, this doesn't cause tail to exit yet. The tail will only exit when it sees the pipe closed, and that only happens when it tries to write to the pipe again, the next time new stuff appears in the file.

Of course if the file constantly fills with new entries, this will happen soon enough.

  • +1. might be best to use tail's -F option instead of -f in case the tomcat startup script rotates the log. BTW, this is the kind of thing I use perl's File::Tail module for, it's perfect for the job - monitor a log file and take actions based on what is seen in the log.
    – cas
    Apr 19, 2021 at 12:46

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.