服务不支持chkconfig命令。

37

程序员们,你们好。我有一个问题,请帮忙。 我正在创建一个服务,它必须在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;
}

但是情况没有改变。问题出在哪里?脚本有什么问题?

5个回答

61

查看chkconfig可以在/etc/rc.d/init.d中打开或关闭的所有脚本,你会注意到顶部的几个注释非常重要。请参阅使用chkconfig和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而言,由于第三行,它看起来像是crond,因此它找不到任何名为listofprocesses的脚本。

您几乎肯定也想更改chkconfig: 2345 90 60。这表示它应该运行的运行级别(在此情况下为2、3、4和5),它的启动顺序(90)和杀死顺序(60)。

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


但是当我更改脚本名称时,它会说服务不支持chkconfig。问题已经解决。我不知道bash想要什么,但在某些时间内,这个脚本已经正常工作了。 - user565447
6
这个答案实际上没有指出所有根本原因。是的,crond行可能部分地有责任,但chkconfig: <run lvl> <start priority> <kill priority> 行存在错误值。启动优先级应该是0-99之间的值 - Jake88
根据 https://serverfault.com/a/29801/67390`# chkconfig: <levels> <start> <stop>

description: <some description>`

这两行都是必须的。
- Christian Müller

23
只需要在最上面添加以下这行代码:
# chkconfig: - 99 10
它就可以奏效了。

4
在哪个位置的顶部? - Dustin Sun
我将其添加到脚本文件的顶部。在我的情况下,该文件位于/etc/init.d/kafka - Jonathan Morales Vélez

4

4

看起来最大优先级是99,在CentOS 6.5上至少如此,这是我目前正在使用的系统。


-1
我也遇到了这个问题,无法在关闭时调用停止函数。在尝试了许多网上的建议后,终于找到了解决方案。 您需要在脚本中添加“touch /var/lock/subsys/”以启动和“rm -f /var/lock/subsys/”以停止功能。由于锁定可能在关闭期间不可用,因此第一次重新启动可能无法正常工作,但从下一次重新启动开始就会开始工作。

Enjoy....:)

Satya


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接