【问题标题】:Service doesn't support chkconfig服务不支持 chkconfig
【发布时间】:2011-02-27 13:50:08
【问题描述】:

程序员们,你们好。我有个问题。请帮忙。 我正在创建一个服务,它必须在加载 Linux 时自动加载。因此,我将脚本复制到目录 /etc/rc.d/init.d 或 /etc/init.d/ 中。但是当我执行命令时

chkconfig --add listOfProcesses

发生错误:

service  listOfProcesses doesn't support chkconfig

这是脚本的内容。我在 Google 中找到了第一个版本,并将其用作模式。

#!/bin/bash
# listOfProcesses   Start the process which will show the list of processes
# chkconfig: 345 110 02
# description: This process shows current time and the list of processes
# processname: listOfProcesses
### BEGIN INIT INFO
# Provides:
# Required-Start:
# Required-Stop:
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: shows current time and the list of processes
# Description: This process shows current time and the list of processes
### END INIT INFO
# Source function library.
KIND="listOfProcesses"
    start() {
            echo -n $"Starting $KIND services: "
            daemon /home/myscript
            echo
    }   

    stop() {
            echo -n $"Shutting down $KIND services: "
            killproc /home/myscript
            echo
    }   

    restart() {
                echo -n $"Restarting $KIND services: "   
                   killproc /home/myscript
               daemon /home/myscript
               echo
    }   

    case "$1" in
      start)
              start
            ;;
      stop)
              stop
            ;;
      restart)
              restart
            ;;
      *)
            echo $"Usage: $0 {start|stop|restart}"
            exit 1
    esac
    exit $?

exit 0;

第二个版本是由 cron 脚本制作的。我找到了 cron 脚本,复制并更改了它,因此我将其用作模式。

#!/bin/sh
#
# crond          Start/Stop the cron clock daemon.
#
# chkconfig: 2345 90 60
# description: cron is a standard UNIX program that runs user-specified \
#              programs at periodic scheduled times. vixie cron adds a \
#              number of features to the basic UNIX cron, including better \
#              security and more powerful configuration options.

### BEGIN INIT INFO
# Provides: crond crontab
# Required-Start: $local_fs $syslog
# Required-Stop: $local_fs $syslog
# Default-Start:  2345
# Default-Stop: 90
# Short-Description: run cron daemon
# Description: cron is a standard UNIX program that runs user-specified 
#              programs at periodic scheduled times. vixie cron adds a 
#              number of features to the basic UNIX cron, including better 
#              security and more powerful configuration options.
### END INIT INFO

rights=whoami;
root=root;
[ -f "$rights"=="$root" ] || { 
echo "this programme requires root rights";
exit 1;
}

# Source function library.
. /etc/rc.d/init.d/functions

start() {
  echo -n $"Starting $KIND services: ";
  daemon showListOfProcesses;
}

stop() {
 echo -n $"Shutting down $KIND services: ";
 killproc showListOfProcesses;
}

restart() {
stop
start
}

reload() {
    restart;
}

force_reload() {
    # new configuration takes effect after restart
    restart
}

case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
restart)
     restart
    ;;
reload)
    reload
    ;;
force-reload)
    force_reload
    ;;
*)
    echo $"Usage: $0 {start|stop|restart|reload|force-reload}"
    exit 2
esac
exit $?

# Show the list of processes
function showListOfProcesses {
  top > /dev/tty2;
}

但情况并没有改变。问题是什么?脚本有什么问题?

【问题讨论】:

    标签: linux bash


    【解决方案1】:

    查看/etc/rc.d/init.dchkconfig 可以打开或关闭的所有脚本,您会发现前几个cmets 非常重要。见How-To manage services with chkconfig and service

    #!/bin/sh
    #
    # crond          Start/Stop the cron clock daemon.
    #
    # chkconfig: 2345 90 60
    # description: cron is a standard UNIX program that runs user-specified \
    #              programs at periodic scheduled times. vixie cron adds a \
    #              number of features to the basic UNIX cron, including better \
    #              security and more powerful configuration options.
    

    您有一个名为 listofprocesses 的脚本,但对于 chkconfig,由于第 3 行,此脚本看起来像 crond,因此它找不到任何名为 listofprocesses 的脚本

    您肯定也想更改chkconfig: 2345 90 60。其中说明了它应该在哪个运行级别(在本例中为 2、3、4 和 5),它的启动顺序是什么(90),它的终止顺序是什么(60)。

    您可以使用chkconfig --list listofprocesses检查服务是否正确设置。

    【讨论】:

    • 但是当我更改脚本的名称时,它说服务不支持 chkconfig。问题已解决。我不知道 bash 想要什么,但在一段时间内这个脚本运行正常
    • 这个答案实际上并没有指出所有根本原因。是的,crond 行可能是部分原因,但chkconfig: <run lvl> <start priority> <kill priority> 行的价值很差。 开始优先级应该是一个值 0-99
    • 根据serverfault.com/a/29801/67390 # chkconfig: <levels> <start> <stop> # description: <some description> 这两行都是必需的。
    【解决方案2】:

    只需在顶部添加以下行: # chkconfig: - 99 10
    它应该可以解决问题

    【讨论】:

    • 顶部在哪里?
    • 我将它添加到脚本文件的顶部。就我而言,此文件位于/etc/init.d/kafka
    【解决方案3】:

    这里是需要在 init 脚本中的元素的出色映射,以实现 chkconfig 和 init 子系统正在做什么,以及每个元素实际做什么:

    http://www.tldp.org/HOWTO/HighQuality-Apps-HOWTO/boot.html

    【讨论】:

      【解决方案4】:

      看起来最大优先级是 99,至少在 CentOS 6.5 上,这是我现在正在玩的。

      【讨论】:

        【解决方案5】:

        我也遇到了这个问题,在关机期间无法调用停止功能。在网上尝试了这么多建议后找到了解决方案。 您需要在脚本中为 start 添加“touch /var/lock/subsys/”,为 stop 函数添加 rm -f /var/lock/subsys/”。第一次重启时 Stop 可能不起作用,因为在关机期间锁定可能不可用,但将从下次重启开始工作。

        享受....:)

        萨提亚

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-01-28
          • 1970-01-01
          • 2017-09-12
          • 2012-09-15
          • 1970-01-01
          • 2020-05-27
          • 2023-03-28
          • 1970-01-01
          相关资源
          最近更新 更多