Exit gracefully when stdout is closed