如何将实时标准输出流拆分为几个文件?

时间:2019-04-18 13:19:13

标签: bash file redirect split stdout

我有一个python脚本,该脚本不断将文本流写入stdout。 像这样的东西(genstream.py):

   while 1:
    print (int(time.time()))
    time.sleep(1)

我想要一个bash脚本来启动python脚本,将其输出保存到一组文件中,比如说每小时分割一次输出,以避免创建难以管理的巨大文件。

然后,将使用相同的bash脚本处理这样创建的文件(即每个小时结束时一个),以将值插入数据库并移至存档文件夹。

我在google / stack溢出中进行了搜索(例如split STDIN to multiple files (and compress them if possible) Bash reading STDOUT stream in real-timehttps://unix.stackexchange.com/questions/26175/),但到目前为止没有找到任何解决方案。

我也尝试过使用类似这样的简单方法(因此,不考虑时间,只考虑行数)

python3 ./genstream.py | split -l5 -

但是我没有输出。

我已经尝试过将(named-)管道和tee组合使用,但是似乎没有任何作用。

1 个答案:

答案 0 :(得分:0)

尝试一下:

python3 ./genstream.py | while read line; do
  echo "$line" >> split_$(date +%Y-%m-%d-%H)
done