Shell脚本分段错误 - AWS

时间:2017-10-23 16:30:39

标签: linux shell amazon-web-services raspberry-pi

我一直在关注将树莓派连接到AWS绿草的教程,并且在最后一步中我一直遇到分段错误。 AWS为我提供了这个greengrassd shell脚本,但是当我运行它时,我遇到了分段错误。我不知道为什么它抛出这个错误所以任何帮助将不胜感激。

AWS Greengrass Tutorial / RaspberryPi

错误

pi@raspberrypi:/greengrass/ggc/packages/1.1.0 $ sudo ./greengrassd start
Setting up greengrass daemon
Validating execution environment
Found cgroup subsystem: cpu
Found cgroup subsystem: cpuacct
Found cgroup subsystem: blkio
Found cgroup subsystem: memory
Found cgroup subsystem: devices
Found cgroup subsystem: freezer
Found cgroup subsystem: net_cls

Starting greengrass daemon./greengrassd: line 158:  2254 Segmentation fault      nohup $COMMAND > /dev/null 2> $CRASH_LOG < /dev/null

Greengrass daemon 2254 failed to start

greengrassd脚本

#!/usr/bin/env bash
##########Environment Requirement for Greengrass Daemon##########
# by default, the daemon assumes it's going to be launched from a directory
# that has the following structure:
# GREENGRASS_ROOT/
#                 greengrassd
#                 bin/daemon
#                 configuration/
#                               group/group.json
#                               certs/server.crt
#                 lambda/
#                        system_lambda1/...
#                        system_lambda2/...
# root cgroup has to be mounted separately, this script doesn't do that for you.
#################################################################

set -e

PWD=$(cd $(dirname "$0"); pwd)
GGC_PKG_HOME=$(readlink -f $PWD)

GG_HOME=$(cd $GGC_PKG_HOME/../../; pwd)
CRASH_LOG=$GG_HOME/var/log/crash.log

GGC_ROOT_FS=$GGC_PKG_HOME/ggc_root
PID_FILE=/var/run/greengrassd.pid
FS_SETTINGS=/proc/sys/fs
GGC_GROUP=ggc_group
GGC_USER=ggc_user

MAX_DAEMON_KILL_WAIT_SECONDS=60
RETRY_SIGTERM_INTERVAL_SECONDS=20

if [ -z "$COMMAND" ]; then
    COMMAND="$GGC_PKG_HOME/bin/daemon -core-dir=$GGC_PKG_HOME -greengrassdPid=$$"
fi

# Function ran as part of initial setup
setup() {
    echo "Setting up greengrass daemon"
    mkdir -p $GGC_ROOT_FS

    # Mask greengrass directory for containers
    mknod $GGC_ROOT_FS/greengrass c 1 3 &>/dev/null || true

    mkdir -p $(dirname "$CRASH_LOG")
}

validatePlatformSecurity() {

    if [[ -f $FS_SETTINGS/protected_hardlinks &&
          -f $FS_SETTINGS/protected_symlinks ]]; then

    PROT_HARDLINK_VAL=$(cat $FS_SETTINGS/protected_hardlinks)
    PROT_SOFTLINK_VAL=$(cat $FS_SETTINGS/protected_symlinks)

    if [[ "$PROT_HARDLINK_VAL" -ne 1 || "$PROT_SOFTLINK_VAL" -ne 1 ]];  then
        echo "AWS Greengrass detected insecure OS configuration: No hardlink/softlink protection enabled." | tee -a $CRASH_LOG
        exit 1
    fi
fi
}

validateEnvironment() {
    echo "Validating execution environment"
    # ensure all commands that the installation script is going to use are available
    if ! type grep >/dev/null ; then
        echo "grep command is NOT on the path or is NOT installed on the system"
        exit 1
    fi

    if ! type cat >/dev/null ; then
        echo "cat command is NOT on the path or is NOT installed on the system"
        exit 1
    fi

    if ! type awk >/dev/null ; then
        echo "awk command is NOT on the path or is NOT installed on the system"
        exit 1
    fi

    if  ! type id >/dev/null ; then
        echo "id command is NOT on the path or is NOT installed on the system"
        exit 1
    fi

    if  ! type ps >/dev/null ; then
        echo "ps command is NOT on the path or is NOT installed on the system"
        exit 1
    fi

    if  ! type sqlite3 >/dev/null ; then
        echo "sqlite3 command is NOT on the path or is NOT installed on the system"
        exit 1
    fi

    # the script needs to be run as root
    if [ ! $(id -u) = 0 ]; then
        echo "The script needs to be run using sudo"
        exit 1
    fi

    if ! id $GGC_USER >/dev/null ; then
        echo "${GGC_USER} doesn't exist. Please add a user ${GGC_USER} on the system"
        exit 1
    fi

    if ! grep -q $GGC_GROUP /etc/group ; then
        echo "${GGC_GROUP} doesn't exist. Please add a group ${GGC_GROUP} on the system"
        exit 1
    fi

    # ensure that kernel supports cgroup
    if [ ! -e /proc/cgroups ]; then
        echo "The kernel in use does NOT support cgroup."
        exit 1
    fi

    # assume that all kernel supported subsystems, which are listed in /proc/cgroups, are going to be used
    # so check whether all of them are mounted.
    for d in `awk '$4 == 1 {print $1}' /proc/cgroups`; do
        if cat /proc/self/cgroup | grep -q $d; then
            echo "Found cgroup subsystem: $d"
        else
            # exit with error if can't find cgroup
            echo "The cgroup subsystem is not mounted: $d"
            exit 1
        fi
    done
}

finish() {
    pid=$1
    echo "$pid" > $PID_FILE
    echo ""
    echo -e "\e[0;32mGreengrass successfully started with PID: $pid\e[0m"
    exit 0
}

start() {
    setup
    if [[ $INSECURE -ne 1 ]]; then
        validatePlatformSecurity
    fi

    validateEnvironment

    trap 'finish $pid' SIGUSR1

    echo ""
    echo -n "Starting greengrass daemon"
    if nohup $COMMAND >/dev/null 2>$CRASH_LOG < /dev/null &
    then
        pid=$!
        # sleep 10 seconds to wait for daemon to start or exit
        sleep 10 &
        wait $!

        echo ""
        echo "Greengrass daemon $pid failed to start"
        echo -e "\e[0;31m$(cat $CRASH_LOG)\e[0m"
        exit 1
    else
        echo "Failed to start Greengrass daemon"
        exit 1
    fi
}

version() {
    $GGC_PKG_HOME/bin/daemon --version
}

stop() {
     if [ -f $PID_FILE ]; then
         PID=$(cat $PID_FILE)
         echo "Stopping greengrass daemon of PID: $PID"

         if [ ! -e "/proc/$PID" ]; then
             rm $PID_FILE
             echo "Process with pid $PID does not exist already"
             return 0
         fi

         echo -n "Waiting"
         kill "$PID" > /dev/null 2>&1
         total_sleep_seconds=0
         until [ "$total_sleep_seconds" -ge "$MAX_DAEMON_KILL_WAIT_SECONDS" ]; do
             sleep 1

             # If the pid no longer exists, we're done, remove the pid file and exit. Otherwise, just increment the loop counter
             if [ ! -e "/proc/$PID" ]; then
                 rm $PID_FILE
                 echo -e "\nStopped greengrass daemon, exiting with success"
                 break
             else
                 total_sleep_seconds=$(($total_sleep_seconds+1))
                 echo -n "."
             fi

             # If it has been $RETRY_SIGTERM_INTERVAL_SECONDS since the last SIGTERM, send SIGTERM
             if [ $(($total_sleep_seconds % $RETRY_SIGTERM_INTERVAL_SECONDS)) -eq "0" ]; then
                 kill "$PID" > /dev/null 2>&1
             fi
         done

         if [ $total_sleep_seconds -ge $MAX_DAEMON_KILL_WAIT_SECONDS ] && [ -e "/proc/$PID" ]; then
            # If we are here, we never exited in the previous loop and the pid still exists. Exit with failure.
            kill -9 "$PID" > /dev/null 2>&1
            echo -e "\nProcess with pid $PID still alive after timeout of $MAX_DAEMON_KILL_WAIT_SECONDS seconds. Forced kill process, exiting with failure."
            exit 1
         fi
     fi
}

usage() {
    echo ""
    echo "Usage: $0 [FLAGS] {start|stop|restart}"
    echo ""
    echo -e "[FLAGS]: \n -i, --insecure \t Run GGC in insecure mode without hardlink/softlink protection, (highly discouraged for production use) \n -v, --version \t\t Outputs the version of GGC."
    echo ""
    exit 1
}

if [[ $# -eq 0 ]]; then
    usage
fi

for var in "$@"
do
    case "$var" in
    -v|--version)
        version
        exit 0
        ;;
    esac
done

while [[ $# -gt 0 ]]
do
    key="$1"
    case $key in
        -i|--insecure)
            mkdir -p $(dirname "$CRASH_LOG")
            echo "Warning! You are running in insecure mode, this is highly discouraged!" | tee -a $CRASH_LOG
            INSECURE=1
            ;;
        -h|--help)
            usage
            ;;
        start)
            stop
            start
            ;;
        stop)
            stop
            ;;
        restart)
            stop
            start
            ;;
        *)
            usage
    esac
    shift
done

1 个答案:

答案 0 :(得分:1)

@Jim也许检查你正在使用的Pi模型?

似乎Green版本的Pi版本适用于ARMv7-A。我也遇到了这个问题,而且我使用的是较旧的Model 1 B +,即ARMv6Z(https://en.wikipedia.org/wiki/Raspberry_Pi#Specifications)。

我们在第158行看到的错误是等待实际进程运行的./greengrassd脚本:

sudo /greengrass/ggc/packages/1.1.0/bin/daemon -core-dir=/greengrass/ggc/packages/1.1.0 -greengrassdPid=641

/greengrass/ggc/packages/1.1.0/bin/daemon是二进制文件。如果直接在控制台中运行上述命令,它将以相同的分段错误错误退出。

AWS确实建议使用Pi 3,所以我猜测它会起作用。