所以我之前问了一个问题,给ping加个前缀。 (我的最后一个问题)给我留下了以下一行:
ping 8.8.8.8 | while read line; do echo "$(date): $line"; done | grep time=
这很管用。我只有一个问题,我无法将其保存在文件中。我尝试了一个简单的重定向,如下所示:
ping 8.8.8.8 | while read line; do echo "$(date): $line"; done | grep time= >> googleping
但文件中没有保存任何内容...
然后我试了一下:
ping 8.8.8.8 | while read line; do echo "$(date): $line"; done | grep time= | tee -a googleping
使用 tee 将其打印在屏幕上并将其保存在文件中……再次失败。
(但尝试了 echo hello | tee -a googleping
并且工作正常......)
然后我尝试了另一个 while 循环:
ping 8.8.8.8 | while read line; do echo "$(date): $line"; done | grep time= | while read line; do echo $line; echo $line >> googleping; done
又一次不走运......
那么一行可以有多少个管道和重定向有限制吗?如果是这样的话,当我无法访问谷歌时,我仍然可以实现我的日志记录目标(我刚刚用 grep time=
测试它以获得 garuenteed 输出并将使用 grep -v time=
获取所有没有时间的行,无论错误是什么)
所以要补充的是,最后我想在 mac 终端中进行操作,但我在 ubuntu 服务器和 mac 上尝试过,但都无法使用上述任何方法。
我希望有人能帮助我!
最佳答案
answer by hek2mgl说明您的特定问题与管道数量无关。
但要回答标题中的问题(“我可以使用多少个管道有限制吗?”),是的opened file descriptors 有限制,但在当前系统上,它在实践中非常大(几千个)。 AFAIU,POSIX 保证小 限制(可能只有 20 个)。您的系统可能每个进程都有一个文件描述符限制,而另一个文件描述符限制系统范围....
要设置或查询每个进程的文件描述符限制,您可以使用 setrlimit(2)和 getrlimit
与 RLIMIT_NOFILE
(和 bash
的内置 ulimit
,或 limit
zsh
的内置,在你的交互式 shell 中)。您还可以在 Linux 上阅读 /proc/self/limits
(有关 /proc/
伪文件的更多信息,请参阅 proc(5))。在我的 Linux Debian 系统上,每个进程最多有 65536 个文件描述符。
IIRC, /proc/sys/fs/file-max
给出了打开的文件描述符的最大数量。在我的系统上,现在是 1632058。
当您达到文件描述符限制时,pipe(2)系统调用(例如,由您的 shell 使用 |
为 pipelines 完成......)将失败:
EMFILE
Too many file descriptors are in use by the process.
ENFILE
The system limit on the total number of open files has been reached.
和open(2)也可能因 disk quota 而失败多余的……
EDQUOT
WhereO_CREAT
is specified, the file does not exist, and the user's quota of disk blocks or inodes on the filesystem has been exhausted.
另见 pipe(7)并阅读 Advanced Linux Programming ;用 C 编程时,你应该使用 fflush(3)适当而明智地。
关于linux - 我可以使用多少个管道有限制吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34476932/