#!/bin/bash

# EXPLAINING THE SCRIPT run.sh
# This is a poor man's supervisord. The only thing this script does is watching its forked (background) processes and as soon as one dies, it terminates all the others and exits with the code of the first dying process.
# see : https://github.com/dinkel/docker-nginx-phpfpm

set -m

php-fpm7.0 &
nginx &

pids=`jobs -p`

exitcode=0

function terminate() {
	trap "" CHLD

	for pid in $pids; do
		if ! kill -0 $pid 2>/dev/null; then
			wait $pid
			exitcode=$?
		fi
	done

	kill $pids 2>/dev/null
}

trap terminate CHLD
wait

exit $exitcode