Joudicek Jouda
Joudicek Jouda

Reputation: 792

Why this Debian-Linux service won't autostart? Starting the service manually after startup works

I'm on a headless RaspberryPi (Raspbian) and I would like this service to autostart when system starts up:

#!/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

The service has 755 permisisons and is owned by root. After setting the permission I did (with no errors):

update-rc.d mnt defaults

When I start the service manually immediately after startup it works well.

Where may be the problem? It would be also great to use this service as a required prerequisite for autostarting Samba - is it possible?

Upvotes: 0

Views: 403

Answers (1)

Joudicek Jouda
Joudicek Jouda

Reputation: 792

The solution was pretty simple. I installed truecrypt only as a binary and I had environment variable path to truecrypt set only for user, not root or any other system user which is used for autostart.

The solution was to change truecrypt command to /path_to_truecrypt/truecrypt.

Upvotes: 1

Related Questions