Below is the command which is executed when I install the application(this line written in one of the script of our application).
PASS=strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 10 | tr -d 'n'
every time I get the error «grep: write error: pipe broken».
here are few points to be noted
- When I install the application on RHEL 7.X. It runs without an issue.
- When I run the command directory on RHEL 8.X. it doesn’t give an error.
- It throws an error only when installing the application on RHEL 8.x.
Also, I have tried few other ways to generate alphanumaric character like:
-
X=
strings /dev/urandom | grep -o -m15 '[[:alnum:]]'
PASS=echo "$X" | head -n 10 | tr -d 'n'
-
PASS=
strings /dev/urandom | tr -dc A-Za-z0-9 | head -c10
-
PASS=
cat /dev/urandom | tr -dc A-Za-z0-9 | head -c10
-
X=
strings /dev/urandom | head -n 100
PASS=echo "X" | grep -o '[[:alnum:]]' | head -n 10 | tr -d 'n'
-
PASS=
< /dev/urandom tr -dc '[[:alnum:]]' | head -c10
None of this worked on RHEL 8.X while installing application. However all this command works fine when executing directly on terminal.
asked Jun 6, 2021 at 14:30
5
The command
PASS=`strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 10 | tr -d 'n'`
does not work due to a bug in strings
. head
exits after having read 10 lines; grep
detects that the other end of its output pipe has been closed, issues grep: write error: Broken pipe
and also exits; strings
ignores that the other end of its output pipe has been closed and blindly continues endlessly.
The commands 3. and 5. which don’t use strings
correctly generate a 10 character password, although 3. also issues write error: Broken pipe
.
answered Jun 6, 2021 at 15:31
ArmaliArmali
17.4k14 gold badges57 silver badges160 bronze badges
3
Seeing «Broken pipe» in this situation is rare, but normal.
When you run type rvm | head -1
, bash executes type rvm
in one process, head -1
in another.1 The stdout of type
is connected to the «write» end of a pipe, the stdin of head
to the «read» end. Both processes run at the same time.
The head -1
process reads data from stdin (usually in chunks of 8 kB), prints out a single line (according to the -1
option), and exits, causing the «read» end of the pipe to be closed. Since the rvm
function is quite long (around 11 kB after being parsed and reconstructed by bash), this means that head
exits while type
still has a few kB of data to write out.
At this point, since type
is trying to write to a pipe whose other end has been closed – a broken pipe – the write() function it caled will return an EPIPE error, translated as «Broken pipe». In addition to this error, the kernel also sends the SIGPIPE signal to type
, which by default kills the process immediately.
(The signal is very useful in interactive shells, since most users do not want the first process to keep running and trying to write to nowhere. Meanwhile, non-interactive services ignore SIGPIPE – it would not be good for a long-running daemon to die on such a simple error – so they find the error code very useful.)
However, signal delivery is not 100% immediate, and there may be cases where write() returns EPIPE and the process continues to run for a short while before receiving the signal. In this case, type
gets enough time to notice the failed write, translate the error code and even print an error message to stderr before being killed by SIGPIPE. (The error message says «-bash: type:» since type
is a built-in command of bash itself.)
This seems to be more common on multi-CPU systems, since the type
process and the kernel’s signal delivery code can run on different cores, literally at the same time.
It would be possible to remove this message by patching the type
builtin (in bash’s source code) to immediately exit when it receives an EPIPE from the write() function.
However, it’s nothing to be concerned about, and it is not related to your rvm
installation in any way.
Я запускаю Ubuntu 14.04.3, это uptodate. Я не знаю, почему, в течение нескольких дней, я начал принимать сообщение grep: write error: Broken pipe о запуске gnome-терминала. Это кажется безобидным, но меня это беспокоит. Как я могу его отладить?
EDIT: я переместил псевдонимы и функции каждый для разделения файлов, таких как .bash_aliases и .bash_functions, и добавил команду, чтобы загрузить их из .bashrc
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
if [ -f ~/.bash_functions ]; then
. ~/.bash_functions
fi
Если я не загружаю .bash_functions проблема исчезает.
Я пытаюсь найти неисправный, отключая каждую функцию по одному.
Это дает мне та же ошибка, но когда я отключил ее, я продолжаю получать ту же ошибку, поэтому у меня могут быть более неисправные функции.
ls -lt $PWD| grep ^d | head -1 | cut -b 51-
grep: development
write error: Broken pipe
Интересно, почему я начинаю принимать эту ошибку.
EDIT2:
Я нашел аналогичную проблему здесь boken pipe
Корень проблемы также кажется схожим.
Я попробовал данную тестовую команду в ссылке, которая имеет ту же ошибку:
bash -c '(while echo foo; do :; done); echo status=$? >&2' | head
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
bash: line 0: echo: write error: Broken pipe
status=0
EDIT3:
Хотя это unbuffer обходное решение я разместил ниже поскольку ответ на мой собственный вопрос работает, я не удовлетворен этим, но мои знания об отладке ограничены. В соответствии с этой ссылкой boken pipe он проистекает из ловушки SIGPIPE другой задачей, и эта ссылка https://lists.gnu.org/archive/html/bug-coreutils/2007-11/msg00154.html точно определяет причину проблемы, это один из модулей проверки подлинности pam, с которыми я недавно столкнулся с проблемой.
задан
16 February 2016 в 18:25
поделиться
1 ответ
В этом ответе суперпользователя есть большое объяснение этой проблемы: как я могу исправить ошибку Broken Pipe?. [! d0]
Команды в трубах выполняются асинхронно: это означает, что в канале, таком как command1 | command2 нет гарантии, что command1 закончится до command2.
При использовании […] | grep | head -n 1, head заканчивается, как только он читает одну строку; если это произойдет до того, как grep закончит запись в трубу, grep получит сигнал SIGPIPE и ошибки.
Как объясняется в ответе ниже, что ответ Суперпользователя, обходным путем является выход на выход из того, что было раньше head в конвейере до tail -n +1, которое игнорирует сигнал SIGPIPE:
command | tail -n +1 | head -n 1
Но в этом случае нет необходимости в head, так как grep имеет возможность распечатывать только первое совпадение:
[...] | grep -m 1
ответ дан Community
23 May 2018 в 13:32
поделиться