Start Perl program, with correct termination signal, and more

Hi all,

iam facing a simple problem.

  • docker-compose should start multiple containers with perl programs
  • perl program should get termination signal at docker stop to save data
  • perl program should auto restart after slow time delay when normally finished
  • perl program should auto restart in case of other errors after bigger time delay

goal is to keep perl running / restarting and quite only at docker stop with correct signal handling to save data

Has anybody an idea to solve this?

My current NOT WORKING solution:

Dockerfile contains:
ENTRYPOINT [“bash”, “startscript.sh”]

startscript.sh:
#!/bin/bash
desc="MyProgram"
to_exec=( perl myprogram.pl  )
trap 'trap_triggered=true' SIGHUP SIGINT SIGTERM
#SIGHUP SIGINT SIGTERM
trap_triggered=false
while ! $trap_triggered; do
   "${to_exec[@]}" &
   job_pid=$!
   wait $job_pid
   job_ret=$?

   if [[ $job_ret = 0 ]]; then
      echo >&2 "MyProgram ended with no errors... (exit code $job_ret) Restarting in 10 Sec..."
      sleep 10s;
#      break
   elif ! $trap_triggered; then
      echo >&2 "Server $desc crashed with exit code $job_ret. Restarting in 1 Min..."
      sleep 1m;
   else
      printf >&2 "Received fatal signal... "
      if kill -0 $job_pid >&/dev/null; then
          printf >&2 "killing job $job_pid... "
          kill $job_pid
          wait $job_pid
      fi
      printf >&2 "quitting container...\n"
   fi
done