nohup命令默认会将输出重定向到当前目录下的nohup.out文件中,但有时候我们需要将多个nohup进程的日志合并输出。以下是几种实现方法:
nohup command1 >> combined.log 2>&1 &
nohup command2 >> combined.log 2>&1 &
这种方法会将所有命令的标准输出和错误输出都追加到同一个combined.log文件中。
nohup command1 2>&1 | tee -a combined.log &
nohup command2 2>&1 | tee -a combined.log &
tee命令可以将输出同时显示在终端和文件中,-a参数表示追加模式。
nohup command1 2>&1 | logger -t myapp &
nohup command2 2>&1 | logger -t myapp &
这样所有日志会进入系统日志,可以使用journalctl -t myapp
查看合并后的日志。
# 先让各个命令输出到不同文件
nohup command1 > cmd1.log 2>&1 &
nohup command2 > cmd2.log 2>&1 &
# 然后用multitail合并查看
multitail cmd1.log cmd2.log
# 创建命名管道
mkfifo /tmp/mypipe
# 启动消费者进程
cat /tmp/mypipe >> combined.log &
# 启动生产者进程
nohup command1 > /tmp/mypipe 2>&1 &
nohup command2 > /tmp/mypipe 2>&1 &
这种方法适合需要实时合并处理日志的场景。