Centos/Fedora/Redhat is confusing on startup scripts as their standards change daily… So I have decided to create a startup script that stops and starts and boots or pauses on virtually any Linux releases. It also sets up Yum to install syncthing.
If you save this script to /etc/init.d/syncthing
# Syncthing /etc/init.d startup by KP
start=/usr/bin/syncthing
lockFile=/var/lock/subsys/syncthing
confFile=./root/.config/syncthing/config.xml
pidFile=/var/run/syncthing.pid
name='syncthing'
#sync_user='nobody'
#sync_user_command='runuser $sync_user'
case "$1" in
'start')
echo "Starting Syncthing"
nohup $sync_user_command sh -c $start >/dev/null 2>&1 & echo $! > $pidFile
RETVAL=$?
if [ "$RETVAL" = "0" ]; then
touch $lockFile >/dev/null 2>&1
fi
echo "Syncthing [Started]"
;;
'stop')
echo "Stopping Syncthing"
for KILLPID in `ps ax | grep '[s]yncthing' | awk ' { print $1;}'`; do
kill -9 $KILLPID >/dev/null 2>&1;
echo "Stopping $KILLPID"
done
echo "Removing lock files $pidFile $lockFile"
rm -f $pidFile
rm -f $lockFile
sleep 1
echo "Syncthing has [Stopped]"
;;
'status')
echo "Status of Syncthing [PID]:"
pidfile=`cat $pidFile`
if [ "$pidfile" != "" ]; then
sleep 1
for KILLPID in `ps ax | grep '[s]yncthing' | awk ' { print $1;}'`; do
echo $KILLPID;
done
else
echo "Not Running"
RETVAL=1
fi
;;
'restart')
$0 stop
$0 start
$0 status
RETVAL=$?
;;
'install')
echo "Installing Syncthing From Fedora Epel"
echo "[daftaupe-syncthing]
name=Copr repo for syncthing owned by daftaupe
baseurl=https://copr-be.cloud.fedoraproject.org/results/daftaupe/syncthing/epel-7-x86_64/
type=rpm-md
skip_if_unavailable=True
gpgcheck=1
gpgkey=https://copr-be.cloud.fedoraproject.org/results/daftaupe/syncthing/pubkey.gpg
repo_gpgcheck=0
enabled=1
enabled_metadata=1" > /etc/yum.repos.d/syncthing.repo
if cat /var/spool/cron/root | grep "syncthing"; then
echo "Cron already patched"
else
echo "Updateing root cron"
echo "@reboot /usr/bin/syncthing > /dev/null 2>&1" >> /var/spool/cron/root
fi
sleep 2
yum install syncthing.x86_64
sleep 2
/usr/bin/syncthing -upgrade-check
/usr/bin/syncthing -version
echo "[Installation Sucessful]"
;;
'pause')
/usr/bin/syncthing -paused
echo "Syncthing has been paused"
;;
'unpaused')
/usr/bin/syncthing -unpaused
echo "Syncthing has been resumed"
;;
*)
echo "Usage: $0 { start | stop | restart | install | pause | unpaused }"
RETVAL=1
;;
esac
exit $RETVAL