我已经在 Linux Mint Debian 版机器上安装了syncthing,并尝试使用/etc/init.d
.我跟着本教程。符号链接/etc/rc.*
存在,当我手动执行脚本(以 root 身份)时,我可以很好地启动和停止守护进程。但是,该脚本不会在引导时启动。
这是脚本:
#!/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
以下是符号链接:
$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
我当前的运行级别是
$/sbin/runlevel
N 2
请注意,我在最顶部插入了几个 echo 语句,这些语句通过管道传输到 /tmp 中的文件中以进行调试。令人惊讶的是,该脚本在启动时执行,但守护程序未启动,并且文件读取
This is /etc/init.d/syncthing
Trying to start daemons...
Trying XXXXXX
HOMEDIR = /home/XXXXXX
config =
当启动后以 root 身份执行脚本来手动启动时,也会创建输出并且输出是相同的。发生了什么?
我不明白的另一件事是该脚本测试文件 $config,但该变量从未定义,因此该文件永远不存在。但有时这个测试似乎评估为真,有时为假?
我缺少什么?
答案1
好吧,我确定了。我的 homedir 使用 ecryptfs 加密,并且仅在我登录时解密。因此在启动期间,二进制文件无法访问 ~/.config/syncthing/* ,它需要正确启动。当然,当我手动启动脚本时,我会登录,所以一切正常。
愚蠢的我。
顺便说一句,start-stop-daemon 的 --no-close 选项提供了提示并允许将守护进程的输出通过管道传输到文件。无论如何,非常感谢您的评论!
恩诺