Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do "tail this file until that process stops" in Bash?

Tags:

bash

shell

I have a couple of scripts to control some applications (start/stop/list/etc). Currently my "stop" script just sends an interrupt signal to an application, but I'd like to have more feedback about what application does when it is shutting down. Ideally, I'd like to start tailing its log, then send an interrupt signal and then keep tailing that log until the application stops.

How to do this with a shell script?

like image 299
Fixpoint Avatar asked Sep 29 '10 07:09

Fixpoint


People also ask

How do I stop a bash script execution?

If you are executing a Bash script in your terminal and need to stop it before it exits on its own, you can use the Ctrl + C combination on your keyboard.

What is $s in bash?

From man bash : -s If the -s option is present, or if no arguments remain after option processing, then commands are read from the standard input. This option allows the positional parameters to be set when invoking an interactive shell. From help set : -e Exit immediately if a command exits with a non-zero status.

How do I continue a line in bash?

From the bash manual: The backslash character '\' may be used to remove any special meaning for the next character read and for line continuation.

What is tail in bash?

By default, the 'tail' command reads the last 10 lines of the file. If you want to read more or less than 10 lines from the ending of the file then you have to use the '-n' option with the 'tail' command.


1 Answers

For just tailing a log file until a certain process stops (using tail from GNU coreutils):

do_something > logfile &
tail --pid $! -f logfile

UPDATE The above contains a race condition: In case do_something spews many lines into logfile, the tail will skip all of them but the last few. To avoid that and always have tail print the complete logfile, add a -n +1 parameter to the tail call (that is even POSIX tail(1)):

do_something > logfile &
tail --pid $! -n +1 -f logfile
like image 90
ndim Avatar answered Oct 09 '22 04:10

ndim