nohup
命令在Linux中用于在后台运行程序,即使关闭终端或断开连接,程序也会继续运行
nohup your_command > output.log 2>&1 &
这里,your_command
是你要运行的命令。>
表示将标准输出重定向到output.log
文件,2>&1
表示将错误输出重定向到标准输出(即output.log
文件),最后的&
表示将命令放入后台运行。
nohup your_command > output.log 2> error.log &
这里,output.log
文件用于存储标准输出,而error.log
文件用于存储错误输出。
nohup your_command > /dev/null 2>&1 &
这里,/dev/null
是一个特殊的设备文件,它会丢弃所有写入其中的数据。因此,将输出重定向到/dev/null
意味着我们不关心程序的输出。
注意:在使用nohup
命令时,建议使用绝对路径来指定要执行的命令,以避免因为当前工作目录的改变而导致命令无法找到。例如,使用/usr/bin/python3 your_script.py
而不是python3 your_script.py
。