I am running the following git clone
command through sudo
and bash
and I want to redirect STDOUT to a log file:
% sudo -u test_user bash -c "git clone https://github.com/scrooloose/nerdtree.git
/home/test_user/.vim/bundle/nerdtree >> /var/log/build_scripts.log"
What is happening is the STDOUT is continuing to be sent to the terminal. i.e.
Cloning into 'nerdtree'...
remote: Counting objects: 3689, done.
[...]
Checking connectivity... done.
I'm guessing the problem has something to do with the fact that sudo
is forking a new process then bash
is forking another, as demonstrated here:
% sudo -u test_user bash -c "{ git clone https://github.com/scrooloose/nerdtree.git
/home/test_user/.vim/bundle/nerdtree >> /var/log/build_scripts.log; ps f -g$$; }"
PID TTY STAT TIME COMMAND
6556 pts/25 Ss 0:02 /usr/bin/zsh
3005 pts/25 S+ 0:00 \_ sudo -u test_user bash -c { git clone https://github.com/scrooloo
3006 pts/25 S+ 0:00 \_ bash -c { git clone https://github.com/scrooloose/nerdtree.
3009 pts/25 R+ 0:00 \_ ps f -g6556
I've tried
- running this in a script and using
exec >> /var/log/build_script.log
before the command - wrapping the command in a function, then calling and redirecting the functions output
But I think these redirections are only applying to the parent and the child processes are defaulting to sending STDOUT to the /dev/tty/25
of their parent causing output to continue to the terminal.
How can I redirect the STDOUT of this command?
2>>&1
but bash is returningbash: -c: line 0: syntax error near unexpected token &'
– the_velour_fog Apr 27 '16 at 08:47&>>
most of the output is lost. Ifgit clone
was sending to STDERR I would have thought these messages would have continued to the terminal when using&>>
but they seem to disappear? – the_velour_fog Apr 27 '16 at 08:512>>&1
. Just usesudo .. git ... >> log 2>&1
that appends. However, it looks likegit
detects that its output is being redirected and stops printing the progress report. You can see the same behavior if you just rungit clone https://github.com/scrooloose/nerdtree.git 2>err
directly. The progress reports are no longer printed. I haven't been able to figure out a way to trickgit
into printing even when redirecting. – terdon Apr 27 '16 at 09:59grep
has the--color=auto
option won't print color if it is being redirected. This is usually a good thing. – terdon Apr 27 '16 at 10:05--progress
option, See updated answer. – terdon Apr 27 '16 at 10:16