为什么这个Debian-Linux服务不会自动启动?启动后手动启动服务

时间:2014-07-29 19:45:19

标签: service debian autostart

我正在使用无头RaspberryPi(Raspbian),我希望这项服务在系统启动时自动启动:

#!/bin/bash
### BEGIN INIT INFO
# Provides:          mnt
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: mount/unmount volumes from /etc/fstab
### END INIT INFO

#VARIABLES for the truecrypt volume
PROTECT_HIDDEN=no
KEYFILES=""
PASSWORD_FILE=/etc/truecrypt

mount_all(){
    slot=0
    while read line;
    do
        read -a fields <<< $line
        VOLUME_PATH=${fields[0]}
        MOUNT_DIRECTORY=${fields[1]}
        FILESYSTEM=${fields[2]}
        OPTIONS=${fields[3]}
        slot=$((slot+1))

        truecrypt \
          --text \
          --verbose \
          --keyfiles=$KEYFILES \
          --protect-hidden=$PROTECT_HIDDEN \
          --slot=${slot} \
          --fs-options=$OPTIONS \
          --filesystem=$FILESYSTEM $VOLUME_PATH $MOUNT_DIRECTORY \
          < <(grep $VOLUME_PATH $PASSWORD_FILE | sed "s,^${VOLUME_PATH}:,,")  \
          | grep -v "Enter password for"

    done < <(grep '^##truecrypt' /etc/fstab | sed 's/##truecrypt://g')

}
# Function to redirect the output to syslog
log_to_syslog(){
    # Temporal file for a named pipe
    script_name=$(basename "$0")
    named_pipe=$(mktemp -u --suffix=${script_name}.$$)

    # On exit clean up
    trap "rm -f ${named_pipe}" EXIT

    # create the named pipe
    mknod ${named_pipe} p

    # start syslog and redirect the named pipe
    # append the script name before the messages
    logger <${named_pipe} -t $0 &

    # Redirect stout and stderr to the named pipe
    exec 1>${named_pipe} 2>&1
}

# If the script does not run on a terminal then use syslog
set_log_output(){
    if [ ! -t 1 ]; then
        log_to_syslog
    fi
}

case "$1" in
    ''|start)
        EXITSTATUS=0
        set_log_output
        mount_all || EXITSTATUS=1
        exit $EXITSTATUS
        ;;
    stop)
        EXITSTATUS=0
        set_log_output
        truecrypt --verbose --force --dismount || EXITSTATUS=1
        exit $EXITSTATUS
        ;;
    restart|force-reload)
        EXITSTATUS=0
        $0 stop || EXITSTATUS=1
        $0 start || EXITSTATUS=1
        exit $EXITSTATUS
        ;;
    status)
        EXITSTATUS=0
        truecrypt --list 2>/dev/null || echo "No truecrypt volumes mounted"
        exit $EXITSTATUS
        ;;
    *)
        echo "Usage: $0 [start|stop|restart]"
        exit 3
        ;;
esac

该服务有755个permisisons,由root拥有。设置权限后我做了(没有错误):

update-rc.d mnt defaults

当我在启动后立即手动启动服务时,它运行良好。

哪里可能有问题?将此服务用作自动启动Samba的必备先决条件也很棒 - 是否可能?

1 个答案:

答案 0 :(得分:1)

解决方案非常简单。我只将truecrypt安装为二进制文件,并且我只为用户设置了truecrypt的环境变量路径,而不是root用户或任何其他用于自动启动的系统用户。

解决方案是将truecrypt命令更改为/path_to_truecrypt/truecrypt