所以我有这些功能,是更大的bash脚本的一部分。问题是我的wait
步骤抛出错误
pid 1不是该shell的子代
function getJobId(){
local result=$(echo "$1" | sed s/^[^0-9]*// | sed s/[^0-9].*$//)
echo $result
}
function deploy(){
echo "Building now"
nohup docker-compose build $1 &> logs/$2.txt &
lastJob=$(getJobId $(jobs %%))
echo "lastjob is --- $lastJob"
jobsList="$jobsList $lastJob"
}
for job in $jobsList
do
wait $job
exit_code=$?
if [[ "$exit_code" != 0 ]]; then
handleError
fi
done
我检查了lastJob
,它包含一个有效值。 bash脚本从GCP ubuntu计算机的终端以root用户身份执行。
答案 0 :(得分:0)
wait
期望pid
,而不是job id
。您最简单(最短?)的更改
代码(未经测试)似乎是:
function deploy(){
echo "Building now"
nohup docker-compose build $1 &> logs/$2.txt &
# lastJob=$(getJobId $(jobs %%))
# as you are exactly creating a new process here, you
# already have the new spawned pid in the $! variable:
lastJob=$!
echo "lastjob is --- $lastJob"
jobsList="$jobsList $lastJob"
}
一个有趣的参考可能是this堆栈溢出问题。