使用 nohup 的一系列命令

series of commands using nohup

我有一系列命令要与 ​​nohup 一起使用。

每个命令可能需要一两天,有时我会与终端断开连接。

实现此目标的正确方法是什么?

方法一:

nohup command1 && command2 && command3 ...

或 方法二:

nohup command1 && nohup command2 && nohup command3 ...

或 方法三:

echo -e "command1\ncommand2\n..." > commands_to_run
nohup sh commands_to_run

我知道方法 3 可能有效,但它迫使我创建一个临时文件。如果我只能从方法一或方法二中选择,哪种方法才是正确的?

nohup command1 && command2 && command3 ...

nohup 将仅适用于 command1。当它完成时(假设它没有失败),command2 将在没有 nohup 的情况下执行,并且容易受到挂断信号的影响。

nohup command1 && nohup command2 && nohup command3 ...

我认为这行不通。这三个命令中的每一个都将受到 nohup 的保护,但是处理 && 运算符的 shell 不会。如果你在 command2 开始之前注销,我认为它不会开始;同样 command3.

echo -e "command1\ncommand2\n..." > commands_to_run
nohup sh commands_to_run

我认为这应该可行 -- 但还有另一种方法不需要创建脚本:

nohup sh -c 'command1 && command2 && command3'

然后 shell 免受挂断信号的影响,我相信三个子命令也是如此。如果我在最后一点上弄错了,你可以这样做:

nohup sh -c 'nohup command1 && nohup command2 && nohup command3'