nohup(no hang-up)命令用于在后台运行程序,即使关闭终端或断开连接,程序也会继续运行。输出重定向是将程序的输出保存到文件中,而不是显示在终端上。以下是一些使用nohup命令进行输出重定向的技巧:
nohup your_command > output.log 2>&1 &
这里,>表示将标准输出重定向到output.log文件,2>&1表示将标准错误输出重定向到标准输出(即output.log文件),最后的&表示在后台运行。
nohup your_command > output.log 2> error.log &
这里,>表示将标准输出重定向到output.log文件,2>表示将标准错误输出重定向到error.log文件。
nohup your_command >> output.log 2>&1 &
这里,>>表示将输出追加到output.log文件,而不是覆盖文件。
tee命令将输出同时显示在终端和保存到文件中:nohup your_command | tee output.log &
这里,|表示将程序的输出传递给tee命令,tee命令会将输出同时显示在终端和保存到output.log文件。
nohup命令运行多个程序,并将它们的输出重定向到不同的文件:nohup your_command1 > output1.log 2>&1 & nohup your_command2 > output2.log 2>&1 &
这里,我们分别运行your_command1和your_command2,并将它们的标准输出和标准错误输出分别重定向到output1.log和output2.log文件。
注意:在使用nohup命令时,建议使用绝对路径来指定程序,以避免因环境变量问题导致的找不到程序的情况。