Redhat / Centos Startup and Install by Yum

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
1 Like

What I usually do is I simply create (vi) this file in: /etc/init.d/syncthing (chmod 755 syncthing)
Than “cd /etc/init.d” and run “./syncthing install” This will install the latest Epel release.
It also sets up cron for you: “@reboot /usr/bin/syncthing > /dev/null 2>&1” so it will always start with the system. However you can also do ./syncthing start or stop or pause without a reboot or “service syncthing restart”…

So you can run this as a service or script or chkconfig or cron and this file can install or update to the latest revision. Enjoy! :wink:

For me this works on every Linux systems:
crontab user -e
@reboot /usr/bin/syncthing > /dev/null 2>&1
(Shift ZZ to save)
But the full script above might provide you a lot more simplicity and control…

I love the syncthing tool! Best tool ever and Thx! :wink:

Hi, I have followed your instructions I have created a file in etc/init.d then I run ./syncthing install

But when I launch my browser and go to “IP_Of_my_server”:8384 I have the error “this site cannot be reached”

I have installed HTTPD server on my redhat what more I have to do ?

Thanks for your answers