Last active
December 28, 2015 01:59
-
-
Save lottspot/7425104 to your computer and use it in GitHub Desktop.
A simple init script for BitTorrent Sync. Written and tested on CentOS 6.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
BIN='/usr/local/bin/btsync' | |
CONF='/usr/local/etc/btsync/sync.conf' | |
PID='/var/run/btsync/btsync.pid' | |
PIDMATCH=true | |
SERVICE=$(basename $0) | |
test $(id -u) -ne 0 && echo "This is for root." >&2 && exit 1 | |
main(){ | |
case $1 in | |
start) | |
if begin | |
then | |
echo "$SERVICE successfully started" | |
exit 0 | |
else | |
echo "Error starting $SERVICE." >&2 | |
exit 1 | |
fi | |
;; | |
stop) | |
if end | |
then | |
echo "$SERVICE successfully stopped" | |
exit 0 | |
else | |
echo "Error stopping $SERVICE" >&2 | |
exit 1 | |
fi | |
;; | |
status) | |
query | |
exit $? | |
;; | |
*) | |
printUsage | |
exit 1 | |
;; | |
esac | |
} | |
running(){ | |
test ! -f $PID && return 1 | |
listen=$(netstat -tnlp | grep btsync | awk '{print $NF}' | cut -d'/' -f1) | |
last=$(cat $PID) | |
test -z $listen && return 1 | |
if [[ $listen -ne $last ]] | |
then | |
echo "Warning: PID of listening instance does not match PID in $PID" >&2 | |
PIDMATCH=false | |
return 0 | |
else | |
return 0 | |
fi | |
} | |
begin(){ | |
if running | |
then | |
echo "$SERVICE is already running." >&2 | |
return 1 | |
else | |
$BIN --config $CONF | |
fi | |
} | |
end(){ | |
if running | |
then | |
if ! $PIDMATCH | |
then | |
echo "Error: Failed to stop $SERVICE: PID in $PID does not match PID of listening instance" >&2 | |
return 1 | |
fi | |
kill -15 $(cat $PID) | |
xstat=$? | |
rm $PID | |
return $xstat | |
else | |
echo "$SERVICE is not running." | |
return 1 | |
fi | |
} | |
query(){ | |
if running | |
then | |
echo "$SERVICE is running" | |
return 0 | |
else | |
echo "$SERVICE is stopped" | |
return 0 | |
fi | |
} | |
printUsage(){ | |
echo "Usage: $0 [start|stop|status]" | |
} | |
main $* |
Line #45 -
listen=$(netstat -tnlp | grep btsync | awk '{print $NF}' | cut -d'/' -f1)
This will return multiple lines and return an exception if you have both the webUI and standard btsync listening port running. You may want to change to :
listen=$(netstat -tnlp | grep btsync | awk '{print $NF}' | cut -d'/' -f1 | head -1)
forked: https://gist.github.com/naegelin/33e95bf415b76531c63f
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any possibility of tweaking this script so it can have a user other than root?
Thanks!