【问题标题】:how to set supervisor to run a shell script如何设置主管运行shell脚本
【发布时间】:2014-04-20 16:43:42
【问题描述】:

设置Dockerfile 来安装节点prereqs,然后设置supervisor 以运行最终的npm install 命令。在 VirtualBox 下的 CoreOS 中运行 Docker。

我有一个 Dockerfile 可以正确设置所有内容:

FROM ubuntu
MAINTAINER <<Me>>

# Install docker basics
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
RUN apt-get update
RUN apt-get upgrade -y

# Install dependencies and nodejs
RUN apt-get update
RUN apt-get install -y python-software-properties python g++ make
RUN add-apt-repository ppa:chris-lea/node.js
RUN apt-get update
RUN apt-get install -y nodejs

# Install git
RUN apt-get install -y git

# Install supervisor
RUN apt-get install -y supervisor
RUN mkdir -p /var/log/supervisor

# Add supervisor config file
ADD ./etc/supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# Bundle app source
ADD . /src

# create supervisord user
RUN /usr/sbin/useradd --create-home --home-dir /usr/local/nonroot --shell /bin/bash nonroot
RUN chown -R nonroot: /src

# set install script to executable
RUN /bin/chmod +x /src/etc/install.sh

#set up .env file
RUN echo "NODE_ENV=development\nPORT=5000\nRIAK_SERVERS={SERVER}" > /src/.env

#expose the correct port
EXPOSE 5000

# start supervisord when container launches
CMD ["/usr/bin/supervisord"]

然后我想设置 supervisord 以启动几个可能的进程之一,包括我已确认可以正常工作的安装 shell 脚本 install.sh,它位于应用程序的 /etc 目录中:

#!/bin/bash
cd /src; npm install
export PATH=$PATH:node_modules/.bin

但是,我对主管语法非常陌生,无法正确启动 shell 脚本。这是我在supervisord.conf 文件中的内容:

[supervisord]
nodaemon=true

[program:install]
command=install.sh
directory=/src/etc/
user=nonroot

当我运行Dockerfile 时,一切运行正常,但是当我启动映像时,我得到以下信息:

2014-03-15 07:39:56,854 CRIT Supervisor running as root (no user in config file)
2014-03-15 07:39:56,856 WARN Included extra file "/etc/supervisor/conf.d/supervisord.conf" during parsing
2014-03-15 07:39:56,913 INFO RPC interface 'supervisor' initialized
2014-03-15 07:39:56,913 WARN cElementTree not installed, using slower XML parser for XML-RPC
2014-03-15 07:39:56,914 CRIT Server 'unix_http_server' running without any HTTP authentication checking
2014-03-15 07:39:56,915 INFO supervisord started with pid 1
2014-03-15 07:39:57,918 INFO spawnerr: can't find command 'install.sh'
2014-03-15 07:39:58,920 INFO spawnerr: can't find command 'install.sh'

很明显,我没有正确设置主管来运行这个 shell 脚本——我搞砸了部分语法吗?

【问题讨论】:

    标签: node.js bash docker supervisord


    【解决方案1】:

    我发现最好的方法是设置这个:

    [program:my-program-name]
    command = /path/to/my/command.sh
    startsecs = 0
    autorestart = false
    startretries = 1
    

    【讨论】:

      【解决方案2】:

      我认为我得到了这个排序:需要command 中的完整路径,而不是.conf 文件中的user=nonroot,我将su nonroot 放入install.sh 脚本中。

      【讨论】:

      • 编辑:没有骰子,看起来它挂在 INFO success: install entered RUNNING state 上,但没有退出脚本。
      【解决方案3】:

      我快速浏览了supervisor 的源代码,发现如果命令 不包含正斜杠/,它将在PATH em> 该文件的环境变量。这模仿了通过 shell 执行的行为。

      以下方法应该可以解决您最初的问题:

      1. 指定脚本的完整路径(就像您在自己的答案中所做的那样)
      2. 命令前缀为./,即./install.sh(理论上,但未经测试)
      3. 命令前加上shell可执行文件,即/bin/bash install.sh

      我不明白为什么user= 对您不起作用(您在修复执行后尝试过吗?),但是您在自己的答案中遇到的问题可能是由于su 的使用不正确导致的像 sudo 一样工作。 su 将创建自己的交互式 shell,因此在等待标准输入时会挂起。要使用 su 运行命令,请使用 -c 标志,即 su -c "some-program" nonroot。如有必要,还可以使用-s 标志指定显式 shell。

      【讨论】:

        猜你喜欢
        • 2014-06-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-31
        • 2011-01-01
        相关资源
        最近更新 更多