在 Linux 中,nohup 命令用于在后台运行程序,即使关闭终端或断开连接,程序也会继续运行。默认情况下,nohup 会将标准输出(stdout)重定向到名为 nohup.out 的文件中,但不会捕获标准错误(stderr)。
要捕获标准错误,你可以使用以下方法之一:
nohup your_command > output.log 2>&1 &
这里,2>&1 表示将标准错误(文件描述符 2)重定向到标准输出(文件描述符 1),& 表示将命令放入后台运行。
nohup your_command > output.log 2> error.log &
这里,2> error.log 表示将标准错误重定向到名为 error.log 的文件中。
tee 命令将标准输出和标准错误都显示在终端上,并将它们重定向到文件。例如:nohup your_command | tee output.log 2>&1 &
这里,| tee output.log 表示将标准输出传递给 tee 命令,tee 命令会将输出同时显示在终端上并写入 output.log 文件。2>&1 表示将标准错误重定向到标准输出,因此标准错误也会被写入 output.log 文件。