---
title: "linux shell - broken pip error"
author: "飞奔的熊猫"
date: "2015-01-07T15:36:55Z"
tags: []
categories: ["开源软件"]
slug: "linux-shell-broken-pip-error"
draft: false
lastmod: "2015-01-07T15:36:55Z"
id: 2173604
url: "https://runpanda.iteye.com/blog/2173604"
---

【基本介绍】
这里我们介绍发生broken pipe错误的原因以及解决方案。

【broken pip error】
例如：svn log svn://xxxx | head -10
这样bash在执行的svn log svn://xxxx的时候起一个线程1，在执行head -10的时候起一个线程2。两个线程同时运行。线程2读到第10行的时候退出，然后关闭管道。但是线程1还在跑，继续往管道里面写数据，但是相对应的线程2的管道已经关闭，所以抛出write error: broken pipe.

【解决方案】
1. 对抛出的异常信号进行特殊处理
2. 先不用管道，把内容先写到临时文件后在处理


【参考引用】
<http://superuser.com/questions/554855/how-can-i-fix-a-broken-pipe-error>
<http://unix.stackexchange.com/questions/60222/why-does-subversion-give-a-broken-pipe-error-when-piped-into-head>