首页 > 解决方案 > 使用分离屏幕模式记录 nodejs 输出

问题描述

我有这个脚本,我想为其记录输出。

for i in {1..20}
do
  sleep 2
  echo "Running Bot $i"
  screen -dm node testbot.js test localhost 4 > $i.txt
done

日志文件被创建,但没有任何内容被放入其中。当我在 shell 中单独运行它们时,它可以工作:

node testbot.js test localhost 4 > test.txt

但它不适用于分离的屏幕。我也尝试了这种变化:

screen -dm node testbot.js test localhost 4 | tee $i.txt

但它也没有用。testbot.js 里面只有一些 console.log() 语句。

我该怎么做才能将输出输出到日志文件中?

标签: node.jsscreenstdout

解决方案


我自己解决了这个问题。我修改了脚本,现在正在记录输出。

for i in {1..20}
do
  sleep 2
  echo "Running Bot $i"
  logfile="$i.txt" 
  export logfile
  screen -mdS bot$i bash -c 'node testbot.js test localhost 4 &> $logfile'  
done

推荐阅读