LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-05-2010, 03:12 PM   #1
ge_shekhar
LQ Newbie
 
Registered: Sep 2010
Posts: 16

Rep: Reputation: 0
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...
 
Old 09-05-2010, 05:29 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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
 
Old 09-05-2010, 05:31 PM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,934

Rep: Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692
Quote:
Originally Posted by ge_shekhar View Post
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 ">>".
 
Old 09-06-2010, 01:40 AM   #4
ge_shekhar
LQ Newbie
 
Registered: Sep 2010
Posts: 16

Original Poster
Rep: Reputation: 0
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...
 
Old 09-06-2010, 01:49 AM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by ge_shekhar View Post
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
 
Old 09-06-2010, 03:50 AM   #6
ge_shekhar
LQ Newbie
 
Registered: Sep 2010
Posts: 16

Original Poster
Rep: Reputation: 0
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..
 
Old 09-06-2010, 04:37 AM   #7
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,606

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
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.
 
Old 09-06-2010, 06:52 AM   #8
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Redirection output kma07 Linux - Newbie 3 05-12-2010 11:13 PM
Output Redirection - Trying to output to screen and file? helptonewbie Linux - Newbie 7 03-19-2009 08:05 AM
Output redirection??? ed_po Linux - Newbie 3 06-13-2006 01:17 PM
Output redirection rmartine Linux - Newbie 2 01-25-2005 07:31 PM
Output redirection legtester Linux - General 4 07-07-2003 03:36 PM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration