I have installed syncthing on a Linux Mint Debian Edition machine and am trying to get it started at boot time using an init script in /etc/init.d
. I followed this tutorial. Symlinks in /etc/rc.*
exist and I can start and stop the daemon just fine when I execute the script manually (as root). However, the script is not started at boot time.
This is the script:
#!/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="XXXXXX"
DAEMON=/opt/syncthing-linux-amd64-v0.10.8/syncthing
echo "This is /etc/init.d/syncthing" > /tmp/syncthing.txt
startd() {
echo "Trying to start daemons..." >> /tmp/syncthing.txt
for stuser in $syncthing_USERS; do
echo "Trying $stuser" >> /tmp/syncthing.txt
HOMEDIR=$(getent passwd $stuser | awk -F: '{print $6}')
echo "HOMEDIR = $HOMEDIR" >> /tmp/syncthing.txt
echo "config = $config" >> /tmp/syncthing.txt
if [ -f $config ]; then
echo "Starting syncthiing 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
Here are the symlinks:
$find /etc/rc* -name "*syncthing*"
/etc/rc0.d/K01syncthing
/etc/rc1.d/K01syncthing
/etc/rc2.d/S01syncthing
/etc/rc3.d/S01syncthing
/etc/rc4.d/S01syncthing
/etc/rc5.d/S01syncthing
/etc/rc6.d/K01syncthing
and my current runlevel is
$/sbin/runlevel
N 2
Note that I have inserted several echo statements at the very top, which are piped into a file in /tmp for debugging. Surprisingly, the script is executed at boot time but the daemon doesn't start, and the file reads
This is /etc/init.d/syncthing
Trying to start daemons...
Trying XXXXXX
HOMEDIR = /home/XXXXXX
config =
When started manually by executing the script as root after booting, the output is also created and the output is identical. What's happening?
Something else I don't understand is that the script tests for a file $config, but this variable is never defined and so the file never exists. But sometimes this test seems to evaluate to true and sometimes false?
What am I missing?
env -i PATH=/sbin:/usr/sbin:/bin:/usr/bin /etc/init.d/syncthing start
? – Gilles 'SO- stop being evil' Nov 26 '14 at 23:05[ -f $config ]
is always true except for some “weird” values ofconfig
, in particular it's true ifconfig
is not defined or empty. It should be[ -f "$config" ]
instead, with double quotes around the variable substitution. As it is, with an emptyconfig
, the script does start the daemon. – Gilles 'SO- stop being evil' Nov 26 '14 at 23:08