orshachar
orshachar

Reputation: 5037

sample spawned processes is alive and exit code

I have bash script that runs a long running process that prints a lot of stuff to stdout. I made the script print the process output only after it finished and only if it failed.

My problem is that some CI require some stdout prints every X minutes and the process sometimes take a lot more than those X minutes.

I'm trying to think of bash solution that would:

  1. run the process and keep the stdout aside.
  2. while the process is running, print "." to stdout every X seconds - to signal that the script is still running.
  3. After the process is done, will be able to get its exit code and recat accordingly.

Here's is what I started writing:

run_process > output.txt 2>&1 & my_pid=$!
exited=0
while [ ${exited} -eq 0 ]
do
  timeout wait 5s ${my_pid};exit_code=$?;exited=1
  echo .
done

clearly, that doesn't work since timeout cannot get a series of commands. Do you have other solutions?

Upvotes: 0

Views: 24

Answers (1)

Jack
Jack

Reputation: 6168

Try this:

run_process > output.txt 2>&1 & my_pid=$!
while ps -p $my_pid > /dev/null; do
  echo -n .
  sleep 5
done

Get exit value from wait:

wait $mypid
echo $?

Upvotes: 0

Related Questions