0

so I have these functions, a part of a larger bash script. And the issue is that my wait step is throwing error

pid 1 is not a child of this 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

I checked the lastJob, and it contains a valid value. The bash script is executed as root from the terminal of a GCP ubuntu machine.

relentless-coder
  • 1,478
  • 3
  • 20
  • 39

1 Answers1

0

wait expects a pid, not a job id. The simplest(shortest?) alteration of your code (untested) seems to be:

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"
}

An interesting reference could be this stack overflow question.

vfalcao
  • 332
  • 1
  • 3
  • 12