bash中带参数执行sh

bash中带参数执行sh

我正在编写一个守护进程来执行服务。语法是./ctlscript.sh start.当我执行它时service openproject start应该运行此命令,但它运行 ./ctlscript.sh 而不带参数,我得到了用法。这是我的脚本:

#! /bin/sh
### BEGIN INIT INFO
# Provides: openproject
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Openprject
# Description: This file starts and stops Openproject server
#
### END INIT INFO

OPENP_DIR=/opt/openprj

case "$1" in
 start)
   su administrador -c $OPENP_DIR/ctlscript.sh start
   ;;
 stop)
   su administrador -c $OPENP_DIR/ctlscript.sh stop
   ;;
 restart)
   su administrador -c $OPENP_DIR/ctlscript.sh stop
   sleep 20
   su administrador -c $OPENP_DIR/ctlscript.sh start
   ;;
 *)
   echo "Usage: openproject {start|stop|restart}" >&2
   exit 3
   ;;
esac

这是我运行 service openproject stop 时得到的结果。当我启动时是一样的./ctlscript.sh(没有任何参数):

usage: /opt/openprj/ctlscript.sh help
       /opt/openprj/ctlscript.sh (start|stop|restart|status)
       /opt/openprj/ctlscript.sh (start|stop|restart|status) mysql
       /opt/openprj/ctlscript.sh (start|stop|restart|status) memcached
       /opt/openprj/ctlscript.sh (start|stop|restart|status) apache
       /opt/openprj/ctlscript.sh (start|stop|restart|status) subversion
       /opt/openprj/ctlscript.sh (start|stop|restart|status) openproject

help       - this screen
start      - start the service(s)
stop       - stop  the service(s)
restart    - restart or start the service(s)
status     - show the status of the service(s)

答案1

的参数-c必须是一个单词,所以

 su administrador -c "$OPENP_DIR/ctlscript.sh start"

对于“重新启动”,应该先“停止”,然后“启动”

相关内容