Skip to content

Commit

Permalink
Merge pull request #1 from kronostechnologies/feat/entrypoint-kill
Browse files Browse the repository at this point in the history
Feat/entrypoint kill
  • Loading branch information
nvanheuverzwijn authored Mar 31, 2017
2 parents 4efb4cc + ac0ae22 commit 7a40872
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ RUN chmod +x /usr/local/bin/entrypoint.sh
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
```

## Flow
Entrypoint follows a specific flow divided in two main part: starting/waiting on services(1,2) and stopping/killing services (3,4).

1. Source all the script in `/k/start.d/` folder sorted according to string numerical value.
2. Execute and wait on the commands passed as parameter (`$@`) or, if no command are passed, sleep infinity. In docker, `$@` correspond to the `CMD` directive.
3. Whenever `SIGTERM`, `SIGQUIT` or `SIGINT` is trapped, source all the script in `/k/stop.d` folder sorted according to string numerical value.
4. Send `SIGTERM` signal to any remaining process, excluding PID 1.

## Environment variable
### ENTRYPOINT_ROOT
If this variable is set, it will change the root directory of the start/stop scripts i.e.:
Expand Down
28 changes: 25 additions & 3 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,34 @@ process_scripts() {
local path=$1

for script in $(ls $path | sort -n); do
echo "> ${path}/${script}... processing"
echo "> Sourcing '${path}/${script}'.."
source "${path}/${script}"
done
}

kill_remaining_process() {
# Gather all PIDs except for pid 1 (entrypoint script) into a
# space separated list, send SIGTERM and wait for those process to finish properly
ps -e > /tmp/ps
local PIDS=`grep -v -E "PID|\s1\s|ps" /tmp/ps | awk 'BEGIN { ORS=" " }; {print $1}'`;
rm /tmp/ps

if [ -n "$PIDS" ]; then
kill -TERM $PIDS
for PID in $PIDS; do
while [[ -d /proc/$PID ]]; do
sleep 0.1
done
done
fi
}

finish() {
trap "" SIGTERM SIGQUIT SIGINT
echo '> Stopping all services..'
process_scripts "${ENTRYPOINT_ROOT}/stop.d"
echo '> Killing remaining process..'
kill_remaining_process
echo '> Shutting down now.'
}

Expand All @@ -28,7 +48,9 @@ echo '> Fully Booted.'
if [[ -n "${@}" ]]; then
echo "> Executing \`${@}\`"
$@ &
wait $!
else
sleep infinity
sleep infinity &
fi
wait $!

finish

0 comments on commit 7a40872

Please sign in to comment.