Skip to content

Instantly share code, notes, and snippets.

@B-Galati
Last active December 15, 2020 08:54
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 B-Galati/7fc9a6bcc80e348f2236af75b8353708 to your computer and use it in GitHub Desktop.
Save B-Galati/7fc9a6bcc80e348f2236af75b8353708 to your computer and use it in GitHub Desktop.
Run vagrant rsync-auto as a daemon
#!/usr/bin/env bash
set -euo pipefail
ROOT_PATH=$(set -e && cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
cd "${ROOT_PATH}"
# Taken from Symfony installer
function output
{
style_start=""
style_end=""
if [ "${2:-}" != "" ]; then
case $2 in
"success")
style_start="\033[0;32m"
style_end="\033[0m"
;;
"error")
style_start="\033[31;31m"
style_end="\033[0m"
;;
"info"|"warning")
style_start="\033[33m"
style_end="\033[39m"
;;
"heading")
style_start="\033[1;33m"
style_end="\033[22;39m"
;;
esac
fi
builtin echo -e "${style_start}${1}${style_end}"
}
function output_success
{
output "${1}" 'success'
}
function output_heading
{
output "${1}" 'heading'
}
function output_info
{
output "${1}" 'info'
}
function output_warning
{
output "${1}" 'warning'
}
function output_error
{
output "${1}" 'error'
}
function kill_children
{
local children
children="$(pgrep -P "$1" || true)"
if [[ -z ${children} ]]; then
return
fi
local pid
for pid in ${children}; do
kill_children "${pid}"
kill -TERM "${pid}" || true
done
}
function kill_vagrant_auto_if_exists
{
if [ ! -f vagrant-rsync-auto.pid ]; then
return
fi
if ! pgrep -F vagrant-rsync-auto.pid >> /dev/null; then
output_info 'vagrant rsync-auto is already stopped'
return
fi
local pid
pid="$(cat vagrant-rsync-auto.pid)"
kill_children "${pid}"
kill -TERM "${pid}"
output_success 'vagrant rsync-auto stopped'
}
function start_vagrant_auto
{
if test -f vagrant-rsync-auto.pid && pgrep -F vagrant-rsync-auto.pid >> /dev/null; then
output_info 'vagrant rsync-auto is already started'
return
fi
nohup vagrant rsync-auto >vagrant-rsync-auto.log 2>vagrant-rsync-auto.err.log &
echo $! > vagrant-rsync-auto.pid
output_success 'vagrant rsync-auto started'
}
case "${1:-}" in
restart)
kill_vagrant_auto_if_exists
start_vagrant_auto
;;
start)
start_vagrant_auto
;;
stop)
kill_vagrant_auto_if_exists
;;
*)
output_info "Usage: ${0} {restart|start|stop}"
exit 1
;;
esac

Use case

Start vagrant rsync-auto automatically when starting a development environment.

Usage

vagrant-rsync-auto.sh {restart|start|stop}

Debug

  • File vagrant-rsync-auto.log records stdout
  • File vagrant-rsync-auto.err.log records stderr

Behavior

  • File vagrant-rsync-auto.pid keeps track of the current vagrant rsync-auto process
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment