linux - Shell 脚本段错误 - AWS

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

我一直在学习有关将树莓派连接到 AWS greengrass 的教程,但我在最后一步一直遇到段错误。 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

最佳答案

@Jim 也许检查一下你使用的 Pi 的型号?

Greengrass 的 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,所以我猜它会在上面工作。

关于linux - Shell 脚本段错误 - AWS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46894224/

相关文章:

linux - uniq -c 不能与 awk 一起使用吗?

javascript - S3 : How to upload a large file using S3 in nodejs using aws-sdk

amazon-web-services - 每周一和每月 cron 表达式无法正常工作

c - libaio : getting event error code

linux - 连续的内存块如何减少内存访问时间?

r - TCGA2STAT R 包在 windows 中不起作用 : How to resolve?

linux - 在 Bash 脚本中嵌入密码

bash - 将输出从文件重定向到标准输出

.net - 是否有适用于 AWS 的高级 .Net SDK?

linux - 对于 vCPU,为什么型号名称与服务器的体系结构不一致/proc/cpuinfo?