redirect script output from within the script to log file and screen at the same time

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I've been trying to redirect and append my script output from within the script to a log file and the screen at the same time. I've tried various guide I could find but none was successful.



My script looks something like this;




#!/bin/bash
log_file=/var/log/test.log
exec 2>&1 | tee -a $log_file

do something...

cleanup ()

do some cleanup...


int_handler()

echo "Interrupted."
cleanup
exit 1

trap 'int_handler' INT

read -r -p "Do you want continue the process now? [y|n] : " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
then
do this...
exit 0
else
do that...
exit 0
fi

while true
do
sleep 1
done
exit 0


each time I run it, it appears that the redirection only capture the last screen output session.



Any help would be greatly appreciated.



Thanks in advance.










share|improve this question























  • What do you mean by "last screen output session"? A more minimal example would help. Also, read man stdbuf.
    – Ferenc Wágner
    Sep 29 '17 at 15:00














up vote
0
down vote

favorite












I've been trying to redirect and append my script output from within the script to a log file and the screen at the same time. I've tried various guide I could find but none was successful.



My script looks something like this;




#!/bin/bash
log_file=/var/log/test.log
exec 2>&1 | tee -a $log_file

do something...

cleanup ()

do some cleanup...


int_handler()

echo "Interrupted."
cleanup
exit 1

trap 'int_handler' INT

read -r -p "Do you want continue the process now? [y|n] : " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
then
do this...
exit 0
else
do that...
exit 0
fi

while true
do
sleep 1
done
exit 0


each time I run it, it appears that the redirection only capture the last screen output session.



Any help would be greatly appreciated.



Thanks in advance.










share|improve this question























  • What do you mean by "last screen output session"? A more minimal example would help. Also, read man stdbuf.
    – Ferenc Wágner
    Sep 29 '17 at 15:00












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I've been trying to redirect and append my script output from within the script to a log file and the screen at the same time. I've tried various guide I could find but none was successful.



My script looks something like this;




#!/bin/bash
log_file=/var/log/test.log
exec 2>&1 | tee -a $log_file

do something...

cleanup ()

do some cleanup...


int_handler()

echo "Interrupted."
cleanup
exit 1

trap 'int_handler' INT

read -r -p "Do you want continue the process now? [y|n] : " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
then
do this...
exit 0
else
do that...
exit 0
fi

while true
do
sleep 1
done
exit 0


each time I run it, it appears that the redirection only capture the last screen output session.



Any help would be greatly appreciated.



Thanks in advance.










share|improve this question















I've been trying to redirect and append my script output from within the script to a log file and the screen at the same time. I've tried various guide I could find but none was successful.



My script looks something like this;




#!/bin/bash
log_file=/var/log/test.log
exec 2>&1 | tee -a $log_file

do something...

cleanup ()

do some cleanup...


int_handler()

echo "Interrupted."
cleanup
exit 1

trap 'int_handler' INT

read -r -p "Do you want continue the process now? [y|n] : " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
then
do this...
exit 0
else
do that...
exit 0
fi

while true
do
sleep 1
done
exit 0


each time I run it, it appears that the redirection only capture the last screen output session.



Any help would be greatly appreciated.



Thanks in advance.







debian






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 29 '17 at 14:57









Ferenc Wágner

2,774920




2,774920










asked Sep 25 '17 at 22:38









user3289583

1




1











  • What do you mean by "last screen output session"? A more minimal example would help. Also, read man stdbuf.
    – Ferenc Wágner
    Sep 29 '17 at 15:00
















  • What do you mean by "last screen output session"? A more minimal example would help. Also, read man stdbuf.
    – Ferenc Wágner
    Sep 29 '17 at 15:00















What do you mean by "last screen output session"? A more minimal example would help. Also, read man stdbuf.
– Ferenc Wágner
Sep 29 '17 at 15:00




What do you mean by "last screen output session"? A more minimal example would help. Also, read man stdbuf.
– Ferenc Wágner
Sep 29 '17 at 15:00















active

oldest

votes











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f394424%2fredirect-script-output-from-within-the-script-to-log-file-and-screen-at-the-same%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f394424%2fredirect-script-output-from-within-the-script-to-log-file-and-screen-at-the-same%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?