温馨提示×

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

小樊
38
2026-01-06 05:32:36
栏目: 智能运维

在 CentOS 中,你可以使用 nohup 命令结合 & 符号在后台运行多个命令

方法一:使用分号(;)分隔命令

nohup command1 ; nohup command2 ; nohup command3 &

方法二:将命令写入一个脚本文件,然后使用 nohup 运行该脚本

  1. 创建一个名为 run_commands.sh 的脚本文件:
#!/bin/bash
command1
command2
command3
  1. 为脚本文件添加可执行权限:
chmod +x run_commands.sh
  1. 使用 nohup 运行脚本:
nohup ./run_commands.sh &

在这两种方法中,& 符号用于将命令放入后台运行。nohup 命令确保即使关闭终端或断开连接,这些命令也会继续运行。运行命令后,你会在当前目录下看到一个名为 nohup.out 的文件,其中包含命令的输出。如果你想将输出重定向到其他文件,可以使用以下语法:

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

这将把每个命令的标准输出和标准错误输出分别重定向到不同的日志文件。

0