LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Shell script-How to call tail then exit when 'END timestamp' appears in log? (https://www.linuxquestions.org/questions/linux-software-2/shell-script-how-to-call-tail-then-exit-when-end-timestamp-appears-in-log-722965/)

Mountain 05-01-2009 11:52 AM

Shell script-How to call tail then exit when 'END timestamp' appears in log?
 
I need to write a script that will log in to a remote server, launch a process, and then display the log output from that process and finally end the ssh session and exit the script when the process ends.

My idea for how to do this task is something like this, but I have no idea how to make it end.

Code:

#! /bin/sh
#
echo "starting at `date`"
ssh user1@remote_server nohup /opt/program/bin/program.pl -p parameter1 -q parameter2 --long_parameter3 &

ssh user2@remote_server tail -f /var/log/program/the_log.log

# when the following line appears in the log, I need to to exit this script
#END      2009.05.01 02:40:45
#where the timestamp is any valid date-time in this format.

echo "finished at `date`"
exit 0

The process "program.pl" will be finished at the point where "END" appears in the log, so maybe I could monitor that instead. But I'm not sure how to do that either.

penguiniator 05-01-2009 01:52 PM

One clue might be to use --pid=$$ with tail, which causes tail to exit when pid dies. $$ is the pid of the script. Maybe pipe the tail -f to grep: | grep END && exit?

Mountain 05-01-2009 02:08 PM

Thanks. Both suggestions sound like great ideas.
I would like to use --pid with tail, but the process just shows up as "perl" which is pretty generic.

Here's what I see from top:

Code:

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND           
 5118 root      20  0  195m 155m 2300 R  69  1.9  2:50.70 perl

This gives the correct result, but what if other perl scripts are running? Is there a way to pin this down to the specific perl script I'm running?
Code:

$ pgrep perl
5118

Looking at the other option (pipe the tail -f to grep), is there a way that I can still see the output from tail and also use this method?

Thanks

penguiniator 05-01-2009 02:19 PM

try using tee. It will allow you to send output to stdout and to files. You should be able to use some kind of redirection to send output to a pipe and to stdout.

Mountain 05-01-2009 02:29 PM

thanks. It is good to know about tee. But I'm having trouble conceptualizing how to put all this together...


All times are GMT -5. The time now is 10:20 AM.