如何在Jenkins上配置多个shell脚本并运行它们而无需等待其他脚本

时间:2015-12-02 20:26:51

标签: linux bash shell jenkins ssh

我正在尝试从jenkins运行一个shell脚本,其形式如下

ssh host1 'nohup path/script.sh &'

ssh host2 'nohup path/script.sh &'

ssh host3 'nohup path/script.sh &'

sleep 90

ssh host1 'nohup path/script.sh &'

ssh host2 'nohup path/script.sh &'

ssh host3 'nohup path/script.sh &'

我想同时并行运行前三个脚本,然后暂停一段时间并开始处理接下来的三个脚本,并按照相同的顺序将所有6个脚本打印输出到jenkins构建日志。我使用nohup让脚本在后台运行,并且不想将脚本的日志输出到任何文件。不想使用nohup script.sh >dev/null

1 个答案:

答案 0 :(得分:1)

首先,ssh进程应该在backround中运行,而不是远程进程。其次,使用wait内置而不是sleep(非常不稳定)。

# Create temporary files for the commmand's output
output1=$(mktemp)
output2=$(mktemp)
output3=$(mktemp)

> "$output1" ssh host1 'path/script.sh' &
> "$output2" ssh host2 'path/script.sh' &
> "$output3" ssh host3 'path/startNode.sh' &
wait && cat "$output1" "$output2" "$output3"

# Cleanup tempfiles
rm  "$output1" "$output2" "$output3"

... more stuff

我正在使用临时文件来输出每个命令。一旦完成所有工作,输出就会连接并打印出来。

相关问题