centos

使用nohup在CentOS中运行多个命令

小樊
37
2025-02-25 00:28:26
栏目: 智能运维

在 CentOS 中,nohup 命令用于在后台运行程序,即使关闭终端或断开连接,程序也会继续运行

  1. 使用分号(;)分隔命令:
nohup command1 ; nohup command2 &

这将首先运行 command1,然后在后台运行 command2

  1. 使用双与符号(&&)分隔命令:
nohup command1 && nohup command2 &

这将首先运行 command1,如果 command1 成功执行,则在后台运行 command2

  1. 使用单个 nohup 命令和子 shell:
nohup bash -c 'command1; command2' &

这将在后台运行一个子 shell,依次执行 command1command2

请注意,为了避免输出混乱,建议将每个命令的输出重定向到不同的日志文件中。例如:

nohup command1 > output1.log 2>&1 &
nohup command2 > output2.log 2>&1 &

这将把 command1 的标准输出和错误输出重定向到 output1.log 文件,把 command2 的标准输出和错误输出重定向到 output2.log 文件。

0
看了该问题的人还看了