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?