Skip to content

Commit

Permalink
Initial version.
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfgang Ziegler // fago committed Apr 11, 2014
0 parents commit c55b761
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 0 deletions.
83 changes: 83 additions & 0 deletions syncd
@@ -0,0 +1,83 @@
#!/bin/bash
#------------------------------------------------------------
# initd script for running services without start-stop-daemon
# (c) Wolfgang Ziegler, nuppla@zites.net, drunomics GmbH
#------------------------------------------------------------

# Find config file

CONF_FILE=syncd.conf

while [ ! -e $CONF_FILE ]; do
if [ $PWD = "/" ]; then
echo "Unable to find syncd configuration file \"$CONF_FILE\"."
exit 1
else
cd ..
fi
done

SYNCD_CONFIG_DIR=$PWD
SCRIPT=`readlink -f $0`
DAEMON_NAME=syncd
LINK=`readlink -f $0`
SCRIPT_DIR=`dirname $LINK`

. $CONF_FILE

COMMAND="rsync $RSYNC_OPTIONS --exclude=$RSYNC_EXCLUDE --delete $WATCH_DIR/ $SSH_USER@$SSH_HOST:$REMOTE_TARGET_DIR"

case $1 in
start)
if [ -e ${PIDFILE} ] && ( ps -p `cat $PIDFILE` > /dev/null ); then
echo "$DAEMON_NAME is already running."
exit 1;
fi
export WATCH_EXCLUDE
export WATCH_VERBOSE
export WATCH_DIR
$SCRIPT_DIR/watch.sh $COMMAND >> $LOGFILE &
echo "$!" > $PIDFILE
;;

stop)
if [ ! -e ${PIDFILE} ] || ( ! ps -p `cat $PIDFILE` > /dev/null ); then
echo "$DAEMON_NAME is not running."
exit 1;
fi

echo "Stopping $DAEMON_NAME..."
PID=`cat $PIDFILE`
CHILD_PIDS=$(pgrep -P $PID);
kill $PID 2> /dev/null || echo Killing process failed, not running?
# Wait half a second and Kill child PIDs to be sure they are gone.
sleep 0.5
kill $CHILD_PIDS 2> /dev/null
rm $PIDFILE
;;
restart)
$SCRIPT stop
$SCRIPT start
;;

status)
if [ -e ${PIDFILE} ] && ( ps -p `cat $PIDFILE` > /dev/null ); then
echo "$DAEMON_NAME is running."
else
echo "$DAEMON_NAME is not running."
fi
;;

run)
$COMMAND && echo Done.
;;

log)
tail -f $LOGFILE
;;

*)
echo "Usage: $0 {start|stop|restart|status|run|log}"
exit 3
;;
esac
26 changes: 26 additions & 0 deletions syncd.conf
@@ -0,0 +1,26 @@
# Syncd config
#
# Variables:
# - SYNCD_CONFIG_DIR: The directory in which the used syncd.conf is located.
# - SCRIPT_DIR: The directory the syncd script has been installed to.

# The directory being watched & synced.
WATCH_DIR=$SYNCD_CONFIG_DIR/your-directory

# Watch-related options
WATCH_EXCLUDE='(\.git|___jb_)'
# Whether to enable verbosity. If enabled, change events are output.
WATCH_VERBOSE=0

# SSH connection settings
SSH_USER=vagrant
SSH_HOST=hostname.local

# Sync options
REMOTE_TARGET_DIR="/var/www"
RSYNC_EXCLUDE='sites/default/files --exclude files'
RSYNC_OPTIONS="-Cra --out-format='[%t]--%n' --include core"

# Syncd files
LOGFILE=".syncd.log"
PIDFILE=".syncd.pid"
108 changes: 108 additions & 0 deletions watch.sh
@@ -0,0 +1,108 @@
#!/bin/bash
# (c) Wolfgang Ziegler // fago
#
# Inotify script to trigger a command on file changes.
#
# The script triggers the command as soon as a file event occurs. Events
# occurring during command execution are aggregated and trigger a single command
# execution only.
#
# Usage example: Trigger rsync for synchronizing file changes.
# ./watch.sh rsync -Cra --out-format='[%t]--%n' --delete SOURCE TARGET
#
# ./watch.sh rsync -Cra --out-format='[%t]--%n' --include core \
# --WATCH_EXCLUDE=sites/default/files --delete ../web/ vagrant@d8.local:/var/www

######### Configuration #########

EVENTS="CREATE,CLOSE_WRITE,DELETE,MODIFY,MOVED_FROM,MOVED_TO"
COMMAND="$@"

## The directory to watch.
if [ -z "WATCH_DIR" ]; then

This comment has been minimized.

Copy link
@rtaft

rtaft Nov 17, 2016

missing $

WATCH_DIR=.
fi

## WATCH_EXCLUDE Git and temporary files from PHPstorm from watching.
if [ -z "$WATCH_EXCLUDE" ]; then
WATCH_EXCLUDE='(\.git|___jb_)'
fi

## Whether to enable verbosity. If enabled, change events are output.
if [ -z "WATCH_VERBOSE" ]; then

This comment has been minimized.

Copy link
@rtaft

rtaft Nov 17, 2016

missing $

WATCH_VERBOSE=0
fi

##################################

if [ -z "$1" ]; then
echo "Usage: $0 Command"
exit 1;
fi

##
## Setup pipes. For usage with read we need to assign them to file descriptors.
##
RUN=$(mktemp -u)
mkfifo "$RUN"
exec 3<>$RUN

RESULT=$(mktemp -u)
mkfifo "$RESULT"
exec 4<>$RESULT

clean_up () {
## Cleanup pipes.
rm $RUN
rm $RESULT
}

## Execute "clean_up" on exit.
trap "clean_up" EXIT


##
## Run inotifywait in a loop that is not blocked on command execution and ignore
## irrelevant events.
##

inotifywait -m -q -r -e $EVENTS --exclude $WATCH_EXCLUDE --format '%w%f' $WATCH_DIR | \
while read FILE
do
if [ $WATCH_VERBOSE -ne 0 ]; then
echo [CHANGE] $FILE
fi

## Clear $PID if the last command has finished.
if [ ! -z "$PID" ] && ( ! ps -p $PID > /dev/null ); then
PID=""
fi

## If no command is being executed, execute one.
## Else, wait for the command to finish and then execute again.
if [ -z "$PID" ]; then
## Execute the following as background process.
## It runs the command once and repeats if we tell him so.
($COMMAND; while read -t0.001 -u3 LINE; do
echo running >&4
$COMMAND
done)&

PID=$!
WAITING=0
else
## If a previous waiting command has been executed, reset the variable.
if [ $WAITING -eq 1 ] && read -t0.001 -u4; then
WAITING=0
fi

## Tell the subprocess to execute the command again if it is not waiting
## for repeated execution already.
if [ $WAITING -eq 0 ]; then
echo "run" >&3
WAITING=1
fi

## If we are already waiting, there is nothing todo.
fi
done

0 comments on commit c55b761

Please sign in to comment.