在 debian squeeze 上手动安装 monit

在 debian squeeze 上手动安装 monit

我正在运行 Debian squeeze 并且想要安装 monit,问题是存储库中的版本太旧并且没有我需要的一些选项。

我下载了源代码并使用以下命令进行了编译:

 $ tar zxvf monit-x.y.z.tar.gz (were x.y.z denotes version numbers)
 $ cd monit-x.y.z
 $ ./configure (use ./configure --help to view available options)
 $ make && make install

它成功编译,但是当我输入

sudo service monit start

我得到:

monit:无法识别的服务

我如何手动将其安装为服务?谢谢

答案1

首先,您需要使用 sudo 实际运行 make install 来安装它,因此:

sudo make install

然后,您需要在 /etc/init.d 中编写一个启动服务的脚本。Monit 在源目录中附带了一个名为 contrib/rc.monit 的通用脚本。下面是 debian wheezy monit 包附带的脚本(某些路径可能与源编译版本不同,并且 debian 版本有一些额外的“功能”,您可以将其合并)。

#!/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

将其复制到名为 /etc/init.d/monit 的文件中并使该文件可执行。

您还应该创建文件 /etc/default/monit,其中包含以下内容:

# /etc/default/monit

# Defaults for monit initscript.  This file is sourced by
# /bin/sh from /etc/init.d/monit.

# You must set this variable to yes for monit to start
START=yes

# Options to pass to monit
#MONIT_OPTS=

然后您应该能够使用运行的命令来启动它:

sudo service monit start

为了使其在启动时自动启动,请运行以下命令让 debian 将其插入到启动服务中的正确位置。

insserv monit

答案2

仅关于 Ubuntu 12.04.4 和 Monit 的评论:

我疯狂地试图弄清楚为什么 monit 无法像宣传的那样在 Ubuntu 12.04.4 上工作。结果发现apt-get提供的软件包已经过期了。我决定从源代码进行编译。

因此,如果您已经像我一样安装了 Monit,那么您只需下载源 tarball,然后./configure --prefix=/usr执行make && make install.

启动脚本已经/etc/init.d运行良好。因此,您可以继续sudo service monit restart操作。

相关内容