node.js 如何设置主管以运行 shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22421140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to set supervisor to run a shell script
提问by fox
Setting up a Dockerfileto install node prereqs and then set up supervisor in order to run the final npm installcommand. Running Docker in CoreOS under VirtualBox.
设置 aDockerfile以安装节点先决条件,然后设置主管以运行最终npm install命令。在 VirtualBox 下的 CoreOS 中运行 Docker。
I have a Dockerfilethat sets everything up correctly:
我有一个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"]
And then I want to set up supervisord to launch one of a few possible processes, including an installation shell script that I've confirmed to work correctly, install.sh, which is located in the application's /etcdirectory:
然后我想设置 supervisord 来启动几个可能的进程之一,包括一个我已经确认可以正常工作的安装 shell 脚本install.sh,它位于应用程序的/etc目录中:
#!/bin/bash
cd /src; npm install
export PATH=$PATH:node_modules/.bin
However, I'm very new to supervisor syntax, and I can't get it to launch the shell script correctly. This is what I have in my supervisord.conffile:
但是,我对主管语法很陌生,无法正确启动 shell 脚本。这是我的supervisord.conf文件中的内容:
[supervisord]
nodaemon=true
[program:install]
command=install.sh
directory=/src/etc/
user=nonroot
When I run the Dockerfile, everything runs correctly, but when I launch the image, I get the following:
当我运行时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'
Clearly, I have not set up supervisor correctly to run this shell script -- is there part of the syntax that I'm screwing up?
显然,我没有正确设置主管来运行这个 shell 脚本——有没有我搞砸的语法部分?
回答by epineda
The best way that I found was setting this:
我发现的最好方法是设置:
[program:my-program-name]
command = /path/to/my/command.sh
startsecs = 0
autorestart = false
startretries = 1
回答by fox
think I got this sorted: needed the full path in command, and instead of having user=nonrootin the .conffile, I put su nonrootinto the install.shscript.
想我得到这个排序:需要完整路径command和,而不是user=nonroot将在.conf文件中,我把su nonroot进入install.sh脚本。
回答by XA21X
I had a quick look in the source code for supervisorand noticed that if the commanddoes not contain a forward slash /, it will look in the PATHenvironmental variable for that file. This imitates the behaviour of execution via shell.
我快速查看了supervisor的源代码,注意到如果命令不包含正斜杠/,它将在PATH环境变量中查找该文件。这模仿了通过 shell 执行的行为。
The following methods should fix your initial problem:
以下方法应该可以解决您最初的问题:
- Specify the full path of the script (like you have done in your own answer)
- Prefix the commandwith
./, i.e../install.sh(in theory, but untested) - Prefix the commandwith the shell executable, i.e.
/bin/bash install.sh
- 指定脚本的完整路径(就像您在自己的答案中所做的那样)
- 用, 即命令前缀(理论上,但未经测试)
././install.sh - 使用 shell 可执行文件作为命令前缀,即
/bin/bash install.sh
I do not understand why user=does not work for you (have you tried it after fixing execution?), but the problem you encountered in your own answer was probably due to the incorrect usage of suwhich does not work like sudo. suwill create its own interactive shell and will therefore hang while waiting for standard input. To run commands with su, use the -cflag, i.e. su -c "some-program" nonroot. An explicit shell can also be specified with the -sflag if necessary.
我不明白为什么user=对您不起作用(您在修复执行后尝试过吗?),但是您在自己的答案中遇到的问题可能是由于su的不正确使用而导致sudo不起作用。su将创建自己的交互式 shell,因此在等待标准输入时会挂起。要使用su运行命令,请使用-c标志,即su -c "some-program" nonroot。-s如有必要,还可以使用该标志指定显式 shell 。

