While running shell scripts how we can redirect the output of shell scripts into two different files. i.e.STDOUT and STDERR files. If there is some error logs should go to STDERR file and if script successfully runs then logs should be generated under STDOUT file
Asked
Active
Viewed 5,115 times
3
-
This isn't a duplicate, since the linked-to question is more complicated (involves also displaying to the terminal). This question is valuable in that it gives the simpler answer to the simpler question. – Patrick Sanan Dec 15 '20 at 13:17
2 Answers
1
Try this:
echo "test" 1>STDOUT 2>STDERR
Replace echo "test"
with any command or script.
Simple example:
Create script.sh with content:
#!/bin/bash
du -shc /*
Add execution permission:
chmod u+x script.sh
And run it:
./script.sh 1>STDOUT 2>STDERR
Then see each file:
# cat STDOUT
8,6M /bin
39M /boot
0 /dev
4,1M /etc
1,1G /home
0 /initrd.img
0 /initrd.img.old
231M /lib
4,0K /lib64
# cat STDERR
du: cannot access `./proc/7422/task/7422/fd/4': No such file or directory
du: cannot access `./proc/7422/task/7422/fdinfo/4': No such file or directory
du: cannot access `./proc/7422/fd/4': No such file or directory
du: cannot access `./proc/7422/fdinfo/4': No such file or directory
To set up redirection inside script use exec
:
#!/bin/bash
exec 1>STDOUT 2>STDERR
du -shc /*
And simply run script:
./script.sh
Explanation:
1>filename
Redirect stdout to file "filename."
1>>filename
Redirect and append stdout to file "filename."
2>filename
Redirect stderr to file "filename."
2>>filename
Redirect and append stderr to file "filename."
&>filename
Redirect both stdout and stderr to file "filename."
This operator is now functional, as of Bash 4, final release.
M>N
"M" is a file descriptor, which defaults to 1, if not explicitly set.
"N" is a filename.
File descriptor "M" is redirect to file "N."
M>&N
"M" is a file descriptor, which defaults to 1, if not set.
"N" is another file descriptor.
For more information see I/O Redirection

Egor Vasilyev
- 2,513
-
Thanks Egor..Helped me a lot..But when executing the script, output file is getting deleted and creating a new file.Could you please let me know how to create a file all the time when scripts execute. – Roshan Oct 11 '17 at 08:28
-
@Roshan, if you need to append data to log files but not recreate this files, you can use >> instead of > – Egor Vasilyev Oct 11 '17 at 08:57
-
,thanks but I need log files should generate every time when my script will run.As of now my Script will be running in every two mins and I need log files should be generated all the time when my scripts is executed. doesn't want to append in single file but also I want logs files to be created all the time. – Roshan Oct 12 '17 at 02:51
-
@Roshan, try to add timestamp to each filename and script will be generated new files everytime it starts. – Egor Vasilyev Oct 12 '17 at 05:25
0
Example shell script:
#!/bin/bash
echo "Good"
# and something bad I can't do under ordinary user
touch /root/something
Run as:
$ test.sh 1>/tmp/STDOUT 2>/tmp/STDERR
And the content is:
$ cat /tmp/STDOUT
Good
$ cat /tmp/STDERR
touch: cannot touch '/root/something': Permission denied

Jaroslav Kucera
- 9,702
-
Can I pass the line inside the script,if yes then how? I mean If I am executing the scripts without passing the stdout and stderr parameters, is there any way that I will only execute the scripts and the script will create two files named as stdout and stderr files with respective messages. – Roshan Oct 11 '17 at 07:24
-
Actually 1>/tmp/STDOUT and 2>/tmp/STDERR aren't parameters in this case. It's parsed by shell before the execution and it redirects all outputs of executed script (or binary). – Jaroslav Kucera Oct 11 '17 at 07:27
-
okk..So actually my requirement is I don't need to pass on shell..I want it to be written inside my shell scripts – Roshan Oct 11 '17 at 07:29
-
Well, it's not really common. External redirect is proper way. However yes, you can add the 1>>/tmp/STDOUT 2>>/tmp/STDERR to each command you execute. In this case there is >> instead of >, which means not rewrite, but append. – Jaroslav Kucera Oct 11 '17 at 07:36
-
1