运行 init.d 脚本会产生“start-stop-daemon:未找到”

运行 init.d 脚本会产生“start-stop-daemon:未找到”

我尝试使用以下命令启动 monit:

/etc/init.d/monit start

然后我得到错误:

[....] Starting daemon monitor: monit/etc/init.d/monit: 124: /etc/init.d/monit: start-stop-daemon: not found
failed!

打字

which start-stop-daemon

节目

/sbin/start-stop-daemon

/sbin 中的 ls -al 显示

-rwxr-xr-x   1 root root     26740 Jan 21 12:18 start-stop-daemon

编辑:添加脚本

#!/bin/sh

### BEGIN INIT INFO
# Provides:          monit
# Required-Start:    $remote_fs
# Required-Stop:     $remote_fs
# Should-Start:      $all
# Should-Stop:       $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: service and resource monitoring daemon
# Description:       monit is a utility for managing and monitoring
#                    processes, programs, files, directories and filesystems
#                    on a Unix system. Monit conducts automatic maintenance
#                    and repair and can execute meaningful causal actions
#                    in error situations.
### END INIT INFO

set -e

. /lib/lsb/init-functions

DAEMON=/usr/bin/monit
CONFIG="/etc/monit/monitrc"
DELAY="/etc/monit/monit_delay"
NAME=monit
DESC="daemon monitor"
MONIT_OPTS=
PID="/var/run/$NAME.pid"

# Check if DAEMON binary exist
[ -f $DAEMON ] || exit 0

[ -f "/etc/default/$NAME" ] && . /etc/default/$NAME

# For backward compatibility, handle startup variable:
if [ -n "$startup" ]
then
  if [ "$1" = "start" ]
  then
    printf "\tPlease, use START variable in /etc/default/monit\n"
    printf "\tto enable/disable $NAME startup.\n"
  fi

  if [ -z "$START" ] && [ "$startup" -eq 1 ]
  then
    START="yes"
  fi
fi

# For backward compatibility, handle CHECK_INTERVALS variable:
if [ -n "$CHECK_INTERVALS" ]
then
  if [ "$1" = "start" ]
  then
    printf "\tPlease, use MONIT_OPTS variable in /etc/default/monit\n"
    printf "\tto specify command line options for $NAME.\n"
  fi

  MONIT_OPTS="$MONIT_OPTS -d $CHECK_INTERVALS"
fi

MONIT_OPTS="-c $CONFIG $MONIT_OPTS"

monit_not_configured () {
  if [ "$1" != "stop" ]
  then
    printf "\tplease configure $NAME and then edit /etc/default/$NAME\n"
    printf "\tand set the \"START\" variable to \"yes\" in order to allow\n"
    printf "\t$NAME to start\n"
  fi
  exit 0
}

monit_check_config () {
  # Check for emtpy config.
  if [ "`grep -s -v \"^#\" $CONFIG`" = "" ]
  then
    echo "empty config, please edit $CONFIG."
    exit 0
  fi
}

monit_check_perms () {
  # Check the permission on configfile.
  # The permission must not have more than -rwx------ (0700) permissions.

  # Skip checking, fix perms instead.
  /bin/chmod go-rwx $CONFIG
}

monit_delayed_monitoring () {
  if [ -f $DELAY ]
  then
    printf "Warning: Please, set start delay for $NAME in config file\n"
    printf "         and delete $DELAY file.\n"

    if [ ! -x $DELAY ]
    then
      printf "Warning: A delayed start file exists ($DELAY)\n"
      printf "         but it is not executable.\n"
    else
      $DELAY &
    fi
  fi
}

monit_checks () {
  # Check if START variable is set to "yes", if not we exit.
  if [ "$START" != "yes" ]
  then
    monit_not_configured $1
  fi
  # Check for emtpy configfile
  monit_check_config
  # Check permissions of configfile
  monit_check_perms
}

case "$1" in
  start)
    log_daemon_msg "Starting $DESC" "$NAME"
    monit_checks $1
    if start-stop-daemon --start --quiet --oknodo \
                         --pidfile $PID --exec $DAEMON \
                         -- $MONIT_OPTS
    then
      log_end_msg 0
    else
      log_end_msg 1
    fi
    monit_delayed_monitoring
    ;;
  stop)
    log_daemon_msg "Stopping $DESC" "$NAME"
    if start-stop-daemon --retry TERM/5/KILL/5 --oknodo --stop --quiet \
                         --pidfile $PID --exec $DAEMON
    then
      log_end_msg 0
    else
      log_end_msg 1
    fi
    ;;
  reload)
    log_daemon_msg "Reloading $DESC configuration" "$NAME"
    if start-stop-daemon --stop --signal HUP --quiet \
                                --oknodo --pidfile $PID \
                                --exec $DAEMON -- $MONIT_OPTS
    then
      log_end_msg 0
    else
      log_end_msg 1
    fi
    ;;
  restart|force-reload)
    $0 stop
    $0 start
    ;;
  syntax)
    $DAEMON $MONIT_OPTS -t
    ;;
  status)
    status_of_proc -p $PID $DAEMON $NAME
    ;;
  *)
    log_action_msg "Usage: /etc/init.d/$NAME {start|stop|reload|restart|force-reload|syntax|status}"
    ;;
esac

exit 0

答案1

在 Debian 7 上我遇到了类似的错误:daemon: not found

我能够用以下方法修复它:

apt-get install daemon

答案2

  1. 确保添加PATH到您的脚本中并且它包含/sbin.由于init脚本不会PATH与系统的其余部分共享环境变量,因此您需要直接在脚本上设置它并确保/sbin其中存在,例如,添加:

     PATH=/sbin:/bin:/usr/sbin:/usr/bin
    

    在脚本的开头。

  2. 调试脚本以确保 start-stop-daemon 从DAEMON变量中正确读取 monit 的路径。为此,请在脚本的开头添加以下行:

    set -x #echo on
    

    整个事情看起来像

    #!/bin/sh
    set -x #echo on
    ### BEGIN INIT INFO
    # Provides:          monit
    # Required-Start:    $remote_fs
    # Required-Stop:     $remote_fs
    # Should-Start:      $all
    # Should-Stop:       $all
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Short-Description: service and resource monitoring daemon
    # Description:       monit is a utility for managing and monitoring
    #                    processes, programs, files, directories and filesystems
    #                    on a Unix system. Monit conducts automatic maintenance
    #                    and repair and can execute meaningful causal actions
    #                    in error situations.
    ### END INIT INFO
    
    set -e
    
    PATH=/sbin:/rest of your path here.
    

    你的剧本。

    如果DAEMON以某种方式损坏,请尝试用括号将其括起来:

    if start-stop-daemon --start --quiet --oknodo \
                         --pidfile $PID --exec $($DAEMON) \
                         -- $MONIT_OPTS
    

    或者直接添加二进制文件的路径。

        if start-stop-daemon --start --quiet --oknodo \
                             --pidfile $PID --exec /usr/bin/monit \
                             -- $MONIT_OPTS
    
  3. 如果最后一个选项不起作用,请确保二进制文件确实存在。如果是,那么您必须检查 start-stop-daemon 是否确实被允许访问它。查看 chroot 。

答案3

很高兴,你可以修复它apt-get install deamon

就我而言,这是一个缺失的链接:/bin/busybox/sbin/start-stop-daemon

cd /sbin
ln -s ../bin/busybox start-stop-daemon

这解决了它。

答案4

在基于 Fedora 或 RedHat 的系统上,您需要安装 dpkg 作为与基于 debian 的发行版的兼容层。start-stop-daemon由 dpkg 提供。

sudo dnf install dpkg

在 Fedora 33 上

相关内容