Skip to content

Commit f04edca

Browse files
author
Bryan Latten
committed
Merge pull request #7 from bryanlatten/feature-script-terminate
Run.sh: allow clean termination from a child run script
2 parents b35c7c6 + 1ea7b90 commit f04edca

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ Variable | Example | Description
1313

1414
To inject things into the runtime process, add shell scripts (ending in .sh) into the
1515
`/run.d` folder. These will be executed during container start.
16+
17+
- If script terminates with a non-zero exit code, container will stop, terminating with the script's exit code, unless...
18+
- If script terminates with exit code of 99, this will signal the container to stop cleanly. This can be used for multi-stage builds that can be committed

container/root/run.sh

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
11
#!/bin/bash
22
RUN_SCRIPTS=/run.d
3+
STATUS=0
4+
5+
# Run shell scripts (ending in .sh) in run.d directory
6+
7+
# When .sh run scripts fail (exit non-zero), container run will fail
8+
# NOTE: if a .sh script exits with 99, this is our stop signal, container will exit cleanly
39

4-
# Run shell scripts (ending in .sh) in run.d directory, if any
510
for file in $RUN_SCRIPTS/*.sh; do
11+
612
echo "[run.d] executing ${file}"
13+
714
/bin/bash $file
15+
16+
STATUS=$? # Captures exit code from script that was run
17+
18+
if [[ $STATUS == 99 ]]
19+
then
20+
echo "[run.d] exit signalled - ${file}"
21+
exit # Exit cleanly
22+
fi
23+
24+
if [[ $STATUS != 0 ]]
25+
then
26+
echo "[run.d] failed executing - ${file}"
27+
exit $STATUS
28+
fi
29+
830
done
931

10-
echo "[nginx] starting (foreground)"
32+
echo "[nginx] start (foreground)"
1133
exec /usr/sbin/nginx -g "daemon off;"

0 commit comments

Comments
 (0)