1

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?

mcenno
  • 241
  • Which rc is it and what run mode are you in ? –  Nov 26 '14 at 13:37
  • I have updated the post - was it what you were looking for? – mcenno Nov 26 '14 at 13:50
  • Yep, you have it set as 01 so it will be started first. Do you have anything else in rc2 and does the script need anything else to be running before it such as networking ? –  Nov 26 '14 at 14:29
  • I don't think so... Other scripts in rc2 are exim4, dbus, cron, atd, ... around 40 services are started there. – mcenno Nov 26 '14 at 14:55
  • 1
    Try changing the number on the symlink so it is executed last on startup. Like S41syncthing –  Nov 26 '14 at 15:19
  • No luck with that. Renamed the link to S50syncthing to no avail. Renaming or not - I can see a message flying by during boot saying "starting syncthiing for user XXXXXX" (yes, note the typo from the init script!) but nothing is running when the machine is up. – mcenno Nov 26 '14 at 20:54
  • Does it work if you try to start the daemon manually with 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
  • The script is buggy: the test [ -f $config ] is always true except for some “weird” values of config, in particular it's true if config is not defined or empty. It should be [ -f "$config" ] instead, with double quotes around the variable substitution. As it is, with an empty config, the script does start the daemon. – Gilles 'SO- stop being evil' Nov 26 '14 at 23:08

1 Answers1

3

Ok, I nailed it down. My homedir is encrypted using ecryptfs, and it is only decrypted when I log in. So during boot time the binary has no access to ~/.config/syncthing/* which it needs to start up properly. When I start the script manually I am logged in, of course, so everything works.

Stupid me.

Btw, the --no-close option of start-stop-daemon provided the hint and allowed to pipe the daemon's output to a file. Many thanks for your comments, anyway!

Enno

mcenno
  • 241