Can I redirect stdout to a file and the console at same time - redirect

I have Linux application running on the Embedded board. I want to redirect stdout to a text file.
So I use below function
file = freopen("logs.txt","w",stdout);
After capturing I resume the console as
freopen("/dev/ttyAM0", "w", stdout);
My question is
If I want to see my stdout on the console while capturing/redirecting it to file, Is it possible and if yes how to get this?
I also tried using dup2() API but not succeded.

Related

STDERR output in asterisk cli

How can i see the output to STDERR in asterisk CLI? I found that the stderr output is visible in the original asterisk terminal but cannot be seen in the cli which is obtained by asterisk -cvvvvvvvvvr. I want to see the error message of my perl agi script (warn "text") .
You can't see it.
Reason: stderror sended to linux stderror handler of asterisk process. When you connect to asterisk console, you have other proccess which have other stderror handler.
So if you want see errors, you need setup your asterisk startup script to store that errors in some file. Or edit default script /usr/sbin/safe_asterisk to suite your need.
Actualy if you read AGI specification you can see,that your script have send error messages to stdout,preferable using WARNING agi function. That can be archived by redirecting stderror to stdout in script or by writing special handler/wrapper.

Profiling a Perl CGI script that times-out

I have a Perl CGI application that sometimes times out, causing it to be killed by Apache and 504 Gateway Time-out error to be sent to browser. I am trying to profile this application using NYTProf, however I cannot read profile data:
$ nytprofhtml -f www/cgi-local/nytprof.out
Reading www/cgi-local/nytprof.out
Profile data incomplete, inflate error -5 ((null)) at end of input file, perhaps the process didn't exit cleanly or the file has been truncated (refer to TROUBLESHOOTING in the documentation)
I am using sigexit=1 NYTProf option. Here's minimal CGI script that reproduces problem:
#!/usr/bin/perl -d:NYTProf
sleep 1 while 1;
Setting sigexit=1 tells NYTProf to catch the following signals:
INT HUP PIPE BUS SEGV
However, when your CGI script times out, Apache sends SIGTERM. You need to catch SIGTERM:
sigexit=term
To catch SIGTERM in addition to the default signals, use:
sigexit=int,hup,pipe,bus,segv,term
CGI.pm has a debug mode, which you can use to run your program from the command line, and pass your CGI parameters as key/value pairs.
It has another feature that you can use to save your params to a file, and then read that file back in later.
What I've done is added the code to save the params to a file, and run my program, via a browser. This also facilitates my abilty to insure that the browser is sending the correct data.
Then I change the code to read the params from the file, and run it as often as I need until I have everything else debugged.
Once you've got the program running to your satisfaction from the command line, you can run it via nytprof to figure out what is taking all the time.

How can I communicate with an application that does not return to the command prompt?

I need to build a test bench by sending appropriate inputs to an application. However,
once I launch the application, it takes control and does not return to the command
prompt (unless an exit command is executed from the application). In that case
is there any technique by which I can send a command to that application from the Perl
script and interpret the output from that application?
My operating system is Windows.
If it's a GUI application, take a look at the Win32::GuiTest module. It sends events to GUI applications - simulating user input.
For a command line application, I would normally recommend the Expect module. Unfortunately, Expect doesn't work under Windows.
If there is anyway to write or redirect the application output to a file, you can always open that file to process/interpret the output. If you are talking about a command-line application, it should be easy to redirect the terminal output to a file using the '>' and '>>' characters. It may not be as easy with a GUI app, though.

STDOUT redirected externally and no output seen at the console

I have a program which reads the output from external application.The external app gives set of output. My program reads the output from this external app while($line=<handle to external app>) and print it to STDOUT.But the "print $line STDOUT" prints only some lines and when the error has occurred ,print to STDOUT is not working , but my one more logging statement "push #arr,$line" has stored complete output from the external app.From this i got to know STDOUT is not working properly when error happens.
Eg:
if external app output is like:
Starting command
First command executed successfully
Error:123 :next command failed
Program terminated
In here the STDOUT prints only :
Starting command
First command executed successfully
But if I check the array it has complete output including error details. So I guessed STDOUT has been redirected or lost.
So I tried storing STDOUT in the beginning of the program to $old_handle using open and then try to restore it before print statement using select($old_handle) (thinking some thing redirects STDOUT when error happens)
But I was not successfull, I don't know what is wrong here. Please help me.
It's possible the output is being buffered. Try setting
$| = 1;
at the start of your program. This will cause the output to be displayed straight away, rather than being buffered for later.
Just guess, may be because error output doesn't go to STDOUT. Use redirect
first_program |& perl_program
or
first_program 2>&1 | perl_program

How do I get the command prompt do dissapear after starting a program via a batch script?

I have a windows batch file which I run to start a java application. The problem is that I don't want the command prompt output to be visible after the app starts. And not only that,... I don't event want to see it minimised. I don't want it at all. Any ideas?
Cheers!!
Use
start/b javaw.exe ...
If your program is not a console application, you can use START.EXE in your batch file to actually launch the real app. The initial console used to launch the batch file will be closed when the batch file ends.
You could actually probably launch the .bat file with start /b too in order to avoid all console windows.