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?
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.
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.
From the bash manual: The backslash character '\' may be used to remove any special meaning for the next character read and for line continuation.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With