2

Is there any way to tell Linux system put all output(stdout,stderr) to a file? With out using redirection, pipe or modification the how scrips get called.

Just tell the Linux use a file for output.

for example: script test1.sh:

#!/bin/bash
echo "Testing 123 "

If i run it like "./test1.sh" (with out redirection or pipe) i'd like to see "Testing 123" in a file (/tmp/linux_output)

Problem: in the system a binary makes a call to a script and this script call many other scrips. it is not possible to modify each call so If i can modify Linux put "output" to a file i can review the logs.

4 Answers 4

5
#!/bin/bash
exec >file 2>&1
echo "Testing 123 "

You can read more about exec here

Sign up to request clarification or add additional context in comments.

2 Comments

This is require to modification the scrips. I'm looking regardless of what script or application run put out put to a file not to a console screen
@FCa, no, this doesn't require modifying the script, because you can do this in an intermediate script.
0

If you are running the program from a terminal, you can use the command script. It will open up a sub-shell. Do what you need to do. It will copy all output to the terminal into a file. When you are done, exit the shell. ^D, or exit. This does not use redirection or pipes.

Comments

0

You could set your terminal's scrollback buffer to a large number of lines and then see all the output from your commands in the buffer - depending on your terminal window and the options in its menus, there may be an option in there to capture terminal I/O to a file.

Comments

0

Your requirement if taken literally is an impractical one, because it is based in a slight misunderstanding. Fundamentally, to get the output to go in a file, you will have to change something to direct it there - which would violate your literal constraint.

But the practical problem is solvable, because unless explicitly counteracted in the child, the output directions configured in a parent process will be inherited. So you only have to setup the redirection once, using either a shell, or a custom launcher program or intermediary. After that it will be inherited.

So, for example:

cat > test.sh

#/bin/sh
echo "hello on stdout"
rm nosuchfile
./test2.sh

And a child script for it to call

cat > test2.sh

#/bin/sh
echo "hello on stdout from script 2"
rm thisfileisnteither
./nonexistantscript.sh

Run the first script redirecting both stdout and stderr (bash version - and you can do this in many ways such as by writing a C program that redirects its outputs then exec()'s your real program)

./test.sh &> logfile

Now examine the file and see results from stdout and stderr of both parent and child.

cat logfile

hello on stdout
rm: nosuchfile: No such file or directory
hello on stdout from script 2
rm: thisfileisnteither: No such file or directory
./test2.sh: line 4: ./nonexistantscript.sh: No such file or directory

Of course if you really dislike this, you can always always modify the kernel - but again, that is changing something (and a very ungainly solution too).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.