nohup
(no hang-up)命令在 Linux 中用于在后台运行程序,即使关闭终端或断开连接,程序也会继续运行
使用 &
符号将命令放入后台运行:
nohup command1 &
nohup command2 &
nohup command3 &
这将在后台同时运行 command1
、command2
和 command3
。
使用 ;
分隔多个命令:
nohup bash -c 'command1; command2; command3' &
这将在后台依次运行 command1
、command2
和 command3
。
使用 &&
分隔多个命令,只有在前一个命令成功执行时才运行下一个命令:
nohup bash -c 'command1 && command2 && command3' &
这将在后台依次运行 command1
、command2
和 command3
,但仅当 command1
成功执行后,才会运行 command2
,依此类推。
使用 ;
或 &&
结合 &
分隔多个命令,实现多任务并发执行:
nohup bash -c 'command1 & command2 & command3' &
或
nohup bash -c 'command1 && command2 && command3' &
这将在后台同时运行 command1
、command2
和 command3
,并根据使用的分隔符决定它们之间的依赖关系。
注意:在使用 nohup
命令时,建议将输出重定向到一个日志文件,以便在需要时查看程序的输出。例如:
nohup command1 > output.log 2>&1 &
这将把 command1
的标准输出和标准错误输出都重定向到 output.log
文件中。