LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help.. on output redirection (https://www.linuxquestions.org/questions/linux-newbie-8/help-on-output-redirection-830491/)

ge_shekhar 09-05-2010 02:12 PM

Help.. on output redirection
 
Hi there,

I have been browsing since morning to get the correct command to do this but never able to get the correct command i want.

I have an application that write some important log in application screen this application is invoked through a shell script. now i want to redirect screen output to another file by making files for each hour..
eg- all the output of 1 PM to 2 PM should be stored in 2010_09_06_13.txt and all the output of 2 PM to 3 PM should be stored in 2010_09_06_14.txt. any help in this regard will be appreciated...

thanks...

Tinkster 09-05-2010 04:29 PM

Hi, welcome to LQ!

I don't think you can "change" the file standard output is
redirected to on the fly; your only option will be to dice
the file after the fact using tools like sed, awk or perl.


Cheers,
Tink

TB0ne 09-05-2010 04:31 PM

Quote:

Originally Posted by ge_shekhar (Post 4088826)
Hi there,
I have been browsing since morning to get the correct command to do this but never able to get the correct command i want.

I have an application that write some important log in application screen this application is invoked through a shell script. now i want to redirect screen output to another file by making files for each hour..
eg- all the output of 1 PM to 2 PM should be stored in 2010_09_06_13.txt and all the output of 2 PM to 3 PM should be stored in 2010_09_06_14.txt. any help in this regard will be appreciated...

Hard to say without seeing the application, and how it's called through your shell script. Since you wrote this, it should be a very simple thing. In your shell script do:
  1. Time check. Get the current hour/date/whatever (easily done via date command)
  2. Assign hour/date to a variable.
  3. When calling the application, redirect log output to the variable name
  4. Sleep until application called again. Loop through from the beginning, and if the file already exists, append to it by writing with a ">>".

ge_shekhar 09-06-2010 12:40 AM

Hi,

Thanks for the reply. I think i could not explain the problem properly in my previous post, Kindly go through the below lines.

1) I have an application (basically its related to call handling of interactive voice response[IVR]).
2) I invoke the applilcation as- ./<application name>
3) This application writes all the application log in screen buffer which i can see in the application.
4) Now i want to run the application in background and want to write this screen log in a textfile, every hour a text file should be generated which i can monitor through <tail> command.
5) I can not close the application at any point of time to redirect the log in another file because if i close the application all the live trafic on the application gets disconnected.


please help.. Thanks...

Tinkster 09-06-2010 12:49 AM

Quote:

Originally Posted by ge_shekhar (Post 4089181)
4) Now i want to run the application in background and want to write this screen log in a textfile, every hour a text file should be generated which i can monitor through <tail> command.
5) I can not close the application at any point of time to redirect the log in another file because if i close the application all the live trafic on the application gets disconnected.

Which brings us back to my post above. You can't
have an hourly file for re-direction. You can (via
sed, awk, perl) generate chunks out of the one file
which your app constantly writes to.


Cheers,
Tink

ge_shekhar 09-06-2010 02:50 AM

Hi Tink,

It would be very helful for me if you can just give some examle to capture it through perl. assume my application name is davinci.sh

i call the application as- sh davinci.sh



Thanks..

crts 09-06-2010 03:37 AM

Hi,

do you mean something like this
Code:

#!/bin/bash

ext=0
oldtime=$(date +%s)
while read -r line; do
        echo "${line}" >> dummylogs/log.$ext
#date +%s
        if [[ $(( $(date +%s) - $oldtime )) > 3 ]];then
                (( ext++ ))
                oldtime=$(date +%s)
        fi
done < <(your_application)

exit

This is a wrapper-script to that will start your application and change the logfile every 3 seconds. But I consider this a very bad solution, actually. Chances are that it might not catch all the output.
You should follow Tinks suggestion. If your application is not a binary but a script, as the name suggests, then you could rewrite it and do the redirection in your script itself.

i92guboj 09-06-2010 05:52 AM

While theoretically possible I've never done this. Maybe studying bits of the "tee", "tmux" and "screen" tools could help you to roll your own, they all do that with stdout each time you attack or reattach a session. All you need is to find a way to change the stdout file descriptor on-the-fly (gdb should be able to do that as well).

But as someone suggested, and since the program is really a script, and provided that you want to find a scripted solution: why not do it the right way and fix your script instead of wrapping it around a new one? The user above already pointed you in that direction and that's what -in my humble opinion- you should be doing.


All times are GMT -5. The time now is 01:55 PM.