Skip to content

Instantly share code, notes, and snippets.

@deeperton
Created December 7, 2014 16:23
Show Gist options
  • Save deeperton/f53dc64aa7433c3593b0 to your computer and use it in GitHub Desktop.
Save deeperton/f53dc64aa7433c3593b0 to your computer and use it in GitHub Desktop.
btsync deamon
#! /bin/sh
### BEGIN INIT INFO
# Provides: btsync daemon
# Required-Start: $syslog
# Required-Stop: $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: BTSync server daemon
# Description: Daemon script to run a BTSync permanent peer
# Placed in /etc/init.d.
### END INIT INFO
# Author: Nicolas Bernaerts <[email protected]>
# Version:
# V1.0, 06/09/2013 - Creation
# V1.1, 09/09/2013 - Use under-priviledged system user
# V1.2, 07/12/2014 - Anton Artyukh - Update start procedure - check for already runned btsync by 'ps -p'
# description variables
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="BTSync server"
NAME="btsync"
USER=$USER
DAEMON=/usr/local/sbin/btsync
ROOT=/home/$USER/bin/bts/.sync
PIDFILE=$ROOT/sync.pid
# Exit if btsync program is not installed
if [ ! -x "$DAEMON" ] ; then
echo "Binary $DAEMON does not exist. Aborting"
exit 0
fi
# Exit if btsync user home directory doesn't exist
if [ ! -d "$ROOT" ]; then
echo "User $USER does not exist. Aborting"
exit 0
fi
# Function that starts the daemon/service
# 0 - daemon started
# 1 - daemon already running
# 2 - daemon could not be started
do_start()
{
# If needed, start the daemon
if [ -f "$PIDFILE" -a "$(ps -p $(cat $PIDFILE) -o comm=)" = "$NAME" ]
then
echo "$NAME already running"
RETVAL="1"
else
start-stop-daemon --start --quiet --chuid $USER --name $NAME --exec $DAEMON -- --config /etc/btsync.conf
RETVAL="$?"
[ "$RETVAL" = "0" ] && echo "$NAME started"
fi
return "$RETVAL"
}
# Function that stops the daemon/service
# 0 - daemon stopped
# 1 - daemon already stopped
# 2 - daemon could not be stopped
do_stop()
{
# Stop the daemon
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
RETVAL="$?"
[ "$RETVAL" = "0" ] && echo "$NAME stopped"
[ "$RETVAL" = "1" ] && echo "$NAME was not running"
# remove pid file
rm -f $PIDFILE
return "$RETVAL"
}
# deal with different parameters : start, stop & status
case "$1" in
# start service
start)
do_start
;;
# stop service
stop)
do_stop
;;
# restart service
restart)
do_stop
do_start
;;
# unknown command, display help message
*)
echo "Usage : $SCRIPTNAME {start|stop|restart}" >&2
exit 3
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment