【问题标题】:My shell script for starting NGiNX is not working我用于启动 NGiNX 的 shell 脚本不工作
【发布时间】:2012-08-18 15:02:35
【问题描述】:

我在一本书中找到了这个小的 shell 脚本...... NGiNX 只是这个脚本不起作用。因为每次我执行 /etc/init.d/nginx start (即文件所在的位置)时,它都会向我发送以下消息: Usage: /etc/init.d/nginx {start|stop|restart|reload}

出于测试目的,我添加了一个 echo "$1",当我这样做时它会向我发送 -e:/etc/init.d/nginx start 或其他任何内容...

我使用的是 Ubuntu 12.04。

#! /bin/sh
# Author: Ryan Norbauer http://norbauerinc.com
# Modified: Geoffrey Grosenbach http://topfunky.com
# Modified: Clement NEDELCU
# Reproduced with express authorization from its contributors
set –e
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
SCRIPTNAME=/etc/init.d/$NAME
# If the daemon file is not found, terminate the script.
test -x $DAEMON || exit 0
echo "0"
d_start() {
$DAEMON || echo -n " already running"
}

d_stop() {
$DAEMON –s quit || echo -n " not running"
}

d_reload() {
$DAEMON –s reload || echo -n " could not reload"
}
echo "$1"
case "$1" in
    start)
            echo -n "Starting $DESC: $NAME"
            d_start
            echo "."
    ;;
    stop)
            echo -n "Stopping $DESC: $NAME"
            d_stop
            echo "."
    ;;
    reload)
            echo -n "Reloading $DESC configuration..."
            d_reload
            echo "reloaded."
    ;;
    restart)
            echo -n "Restarting $DESC: $NAME"
            d_stop
            # Sleep for two seconds before starting again, this should give the
            # Nginx daemon some time to perform a graceful stop.
            sleep 2
            d_start
            echo "."
            ;;
    *)
            echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
            exit 3
    ;;
esac

exit 0

【问题讨论】:

  • 你用的是什么外壳? sh --version

标签: shell ubuntu nginx ubuntu-12.04


【解决方案1】:

set –e 正在将脚本的参数列表设置为–e。删除该行,脚本将运行。

编辑: 发布的脚本包含 set –e(即 EN DASH + e),而不是 set -e(ASCII 连字符/减号 + e)。这导致sh 覆盖–e 传递的第一个参数,而不是在shell 中设置-e 选项。将 替换为- 应该可以解决问题。

【讨论】:

  • 说实话,我怀疑它会。如果特定的外壳不能吃set -e,它当然不应该支持像echo -n 这样的bashism。
  • 在我的 Ubuntu 12.04 机器上尝试过(/bin/sh -> /bin/dash,这就是解决方法。
  • 很奇怪。我的dashset -e 没有问题……
  • dash 在被称为/bin/sh 时可能表现不同。此外,可以通过执行 /bin/echo 而不是将 echo 作为内置函数来支持 echo -n
  • 原来他发布的脚本包含“-e”(EN DASH e)而不是“-e”(连字符e)。交换字符可以解决问题。
猜你喜欢
  • 2019-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-20
  • 1970-01-01
相关资源
最近更新 更多