Skip to content

Instantly share code, notes, and snippets.

@klondikemarlen
Created February 26, 2020 16:45
Show Gist options
  • Save klondikemarlen/c5a22b3ee89de2161a0f9da5db27beff to your computer and use it in GitHub Desktop.
Save klondikemarlen/c5a22b3ee89de2161a0f9da5db27beff to your computer and use it in GitHub Desktop.
Local file watcher to restart docker container.
#!/usr/bin/env bash
restart_app () {
local app_name="$1"
# consider pinging the server an only sending this if it is alive?
docker kill --signal="SIGUSR1" "$app_name" &> /dev/null
}
# Note: using -qq option with inotify will kill all events. So don't do that :p
app_restart_watcher () {
local app_name="$1"
local dir_to_watch="$2"
local pid_file="$3"
mkdir -p "$(dirname "$pid_file")"
(echo "$BASHPID" > "$pid_file"; \
exec inotifywait -q --monitor --recursive --event modify "$dir_to_watch") | \
while read event; do
# echo "read event" "$event"
# echo 'Trying to restart the app ...'
restart_app "$app_name"
done
}
@klondikemarlen
Copy link
Author

klondikemarlen commented Feb 26, 2020

Require some watcher for the SIGUSR1 signal.

E.g.

Signal.trap('USR1') do
  # If this signal is triggered before the server is up
  # it will kill the container.
  sleep 3
  puts 'Restarting ...'
  exec("bundle exec ruby #{__FILE__}")  # This replaces the current process with a new version of itself.
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment