在Debian系统中,nohup
命令用于在后台运行程序,即使关闭终端或断开连接,程序也会继续运行
使用 &
将命令放入后台运行:
nohup your_command &
例如,要在后台运行名为 my_script.sh
的脚本,可以使用以下命令:
nohup ./my_script.sh &
将输出重定向到文件:
默认情况下,nohup
会将输出重定向到名为 nohup.out
的文件。如果你想将输出重定向到其他文件,可以使用以下命令:
nohup your_command > output_file &
例如,要将输出重定向到名为 output.log
的文件,可以使用以下命令:
nohup ./my_script.sh > output.log &
同时重定向输出和错误:
如果你想同时重定向输出和错误,可以使用以下命令:
nohup your_command > output_file 2>&1 &
例如,要将输出和错误都重定向到名为 output.log
的文件,可以使用以下命令:
nohup ./my_script.sh > output.log 2>&1 &
在 nohup
命令中使用管道:
虽然 nohup
本身不支持管道,但你可以通过子shell来实现这一功能。例如,如果你想将一个命令的输出作为另一个命令的输入,并在后台运行,可以使用以下命令:
nohup bash -c 'command1 | command2' &
例如,如果你想将 ls
命令的输出作为 grep
命令的输入,并在后台运行,可以使用以下命令:
nohup bash -c 'ls | grep "keyword"' &
总之,你可以根据需要组合使用 nohup
、&
、输出重定向和其他命令,以便在Debian系统中实现所需的功能。