Skip to content

Instantly share code, notes, and snippets.

@gongzhang
Last active July 29, 2018 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gongzhang/3aeae356954f3b1b2609b67e2349d8a8 to your computer and use it in GitHub Desktop.
Save gongzhang/3aeae356954f3b1b2609b67e2349d8a8 to your computer and use it in GitHub Desktop.
Multi-Service Container Start-Up Script Template

Multi-Service Container Start-Up Script Template

#!/bin/bash

# Multi-Service Container Start-Up Script Template
#   https://docs.docker.com/config/containers/multi-service_container/

# Start the first process
echo "starting process 1 in screen 'p1'..."
screen -dmS p1 bash -c 'sleep 10'
status=$?
if [ $status -ne 0 ]; then
  echo "Failed to start process 1: $status"
  exit $status
fi

# Start the second process
echo "starting process 2 in screen 'p2'..."
screen -dmS p2 bash -c 'sleep 10'
status=$?
if [ $status -ne 0 ]; then
  echo "Failed to start process 2: $status"
  exit $status
fi

echo "OK, running."
echo "(Use 'screen -ls' and 'screen -r <screen name>' to interact with sub-processes.)"
# Naive check runs checks once a minute to see if either of the processes exited.
# This illustrates part of the heavy lifting you need to do if you want to run
# more than one service in a container. The container exits with an error
# if it detects that either of the processes has exited.
# Otherwise it loops forever, waking up every 60 seconds

while sleep 3; do
  ps aux |grep p1 |grep -q -v grep
  PROCESS_1_STATUS=$?
  ps aux |grep p2 |grep -q -v grep
  PROCESS_2_STATUS=$?
  # If the greps above find anything, they exit with 0 status
  # If they are not both 0, then something is wrong
  if [ $PROCESS_1_STATUS -ne 0 -o $PROCESS_2_STATUS -ne 0 ]; then
    echo "One of the processes has already exited."
    exit 1
  fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment