Shell – Run in background but not discarding the output

background-processshellterminalxterm

I use a Git hosting. So I often run git push.

I want to run git push in background, in order that me not to wait when it finishes but do other tasks in my terminal.

But I don't want to run git push in real background (git push &) because I need to see its output to be sure that it run without an error.

What is the proper way to run it like a background process but seeing its output nevertheless?

The best thing I conveived is:

xterm -hold -e 'git push' &

But this uses XTerm instead of Gnome Terminal, the application I use the most. This is somehow inconsistent.

What are other alternatives?

Best Answer

If you don't need to see the output in real time, you can do something like:

git push 2>&1 > ~/git-push-$(date +"%Y%m%d-%H%M").log &

The above will create a file in your home directory with the date and time you invoked it in its filename (e. g. git-push-20160208-1201.log). You can put this into an alias or shell function so that you don't have to retype, or if you only need to preserve the last push for review, you can just use:

git push 2>&1 > ~/git-push.log &

You can even get really fancy and send yourself a notification if the push fails, with either command:

git push 2>&1 > ~/git-push.log || notify-send "Push failed" "git push initiated from $(pwd) at $(date) threw an error!" &
Related Question