Skip to content

Instantly share code, notes, and snippets.

@arudmin
Last active June 8, 2019 17:44
Show Gist options
  • Save arudmin/5a13e9105814c3f568ec to your computer and use it in GitHub Desktop.
Save arudmin/5a13e9105814c3f568ec to your computer and use it in GitHub Desktop.
/etc/init.d/syncthing script for Raspberry Pi (or any Ubuntu/Debian)
#!/bin/sh
### BEGIN INIT INFO
# Provides: syncthing
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Multi-user daemonized version of syncthing.
# Description: Starts the syncthing daemon for all registered users.
### END INIT INFO
# Replace with users you want to run syncthing clients for
# syncthing_USERS="<your name here>"
syncthing_USERS="pi"
DAEMON=/usr/local/bin/syncthing
startd() {
for stuser in $syncthing_USERS; do
HOMEDIR=$(getent passwd $stuser | awk -F: '{print $6}')
if [ -f $config ]; then
echo "Starting syncthing for $stuser"
start-stop-daemon -b -o -c $stuser -S -u $stuser -x $DAEMON
else
echo "Couldn't start syncthing for $stuser (no $config found)"
fi
done
}
stopd() {
for stuser in $syncthing_USERS; do
dbpid=$(pgrep -fu $stuser $DAEMON)
if [ ! -z "$dbpid" ]; then
echo "Stopping syncthing for $stuser"
start-stop-daemon -o -c $stuser -K -u $stuser -x $DAEMON
fi
done
}
status() {
for stuser in $syncthing_USERS; do
dbpid=$(pgrep -fu $stuser $DAEMON)
if [ -z "$dbpid" ]; then
echo "syncthing for USER $stuser: not running."
else
echo "syncthing for USER $stuser: running (pid $dbpid)"
fi
done
}
case "$1" in
start) startd
;;
stop) stopd
;;
restart|reload|force-reload) stopd && startd
;;
status) status
;;
*) echo "Usage: /etc/init.d/syncthing {start|stop|reload|force-reload|restart|status}"
exit 1
;;
esac
exit 0
@arudmin
Copy link
Author

arudmin commented Jun 9, 2014

Usage

  1. Move syncthing application to the /usr/local/bin folder
sudo mv syncthing /usr/local/bin/
  1. Copy this script to /etc/init.d/ folder and make executable
sudo chmod +x /etc/init.d/syncthing

@Sol-Illadan
Copy link

how to add logging to the script ?

Copy link

ghost commented Apr 30, 2015

start-stop-daemon -b -o -c $stuser -S -u $stuser -x $DAEMON >> 2>&1

@Morrisman68
Copy link

Dumb question from newbie - apologies!

Is it sufficient just to add the script to the /etc/init.d directory or does one need to run update-rc.d as well?

Many thanks.

@henryx
Copy link

henryx commented Jan 4, 2016

Correct way to add logging in the script is:
start-stop-daemon -b -o -c $stuser -S -u $stuser -x $DAEMON -- -logfile="/path/to/logging"

@phil123456
Copy link

apparently not working, it does not create the config.xml

@calmh
Copy link

calmh commented Sep 5, 2016

This script is bad and should not be used. I suspect it's just slightly modified copy pasta from somewhere else, but the end result is nonsensical and mostly works by blind luck. There are valid alternatives in https://github.com/syncthing/syncthing/tree/master/etc and in the release / Debian packages.

Copy link

ghost commented Dec 13, 2016

@Morrisman68 of course you need to :))
sudo update-rc.d syncthing defaults

@frederickjh
Copy link

@calmh Sorry, I cannot find a sysvinit script in either of those places. I find runit, upstart, systemd among others, but no sysvinit script.

@frederickjh
Copy link

A better sysvinit script was submitted in this Syncthing issue: Debian SysVinit Script #2990. The Gist of it can be found here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment