如何调试未运行的 init.d 脚本

如何调试未运行的 init.d 脚本

我正在尝试god在 Debian 上启动。我在/etc/init.d/目录中添加了一个脚本,如下所示。

#!/usr/bin/sh
### BEGIN INIT INFO
# Provides:          god
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog 
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: start god process monitoring
# Description:       Start god process monitoring
### END INIT INFO

god_conf="absolute/path/to/god.conf"
god_pid_file="/var/run/god/god.pid" ; mkdir -p `dirname $god_pid_file`
god_log_file="/var/log/god/god.log" ; mkdir -p `dirname $god_log_file`
case "$1" in
  start)
    god -c "$god_conf" -P "$god_pid_file" -l "$god_log_file"
    RETVAL=$?
    echo "God started"
    ;;
...

我运行以下命令来创建必要的符号链接sudo update-rc.d god defaults

然而,当我重新启动机器时,上帝似乎并没有开始。我的问题是,如何调试这个问题?

我看过了dmesg,没有看到任何与 相关的内容god。我也看过了,syslog但没有看到任何东西。另外,该god.log文件是空的,没有god.pid文件。

我不知道问题是否在于我的初始化脚本甚至没有被调用,或者它在尝试启动上帝时是否失败。我尝试通过运行手动启动上帝sudo /etc/init.d/god start,这似乎工作正常。有任何想法吗?

答案1

#!/usr/bin/sh

sh在几乎所有 UNIX 中,其正常位置是/bin/sh。一些unix变体已经合并了/bin/usr/bin,所以/usr/bin/sh也可以工作,但是可移植的方式是/bin/sh,所以这就是你应该在shebangs中使用的方式。对您来说最重要的是,在 Debian 上,除非您不遗余力,否则不会有/usr/bin/sh.

答案2

我会利用通过谷歌找到的这个版本,而不是尝试自己推出。

摘抄

#! /bin/sh
### BEGIN INIT INFO
# Provides:          god
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: God initscript
### END INIT INFO

# This is a Generated Init Script see the source: https://github.com/donnoman/cap-recipes/blob/master/lib/cap_recipes/tasks/god/god.init

# Author: Johnny Domino ([email protected])
# Adapted: Donovan Bray ([email protected])

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH="/custom/ree/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"
DESC="God Monitoring Tool"
NAME=god
CONF=/var/www/application/current/config/daemons.god
LEVEL=info
LOGFILE="/var/www/application/shared/log/god.log" ; mkdir -p `dirname $LOGFILE`
PIDFILE="/var/www/application/shared/pids/god.pid" ; mkdir -p `dirname $PIDFILE`
DAEMON=/var/www/application/current/bin/god
DAEMON_ARGS="-c $CONF -P $PIDFILE --log-level $LEVEL --log $LOGFILE"
SCRIPTNAME=/etc/init.d/god
OPEN_SOCKET=yes
USE_TERMINATE_ON_KILL=yes

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
    # Return
    #   0 if daemon has been started
    #   1 if daemon was already running
    #   2 if daemon could not be started
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
        || return 1
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
        $DAEMON_ARGS \
        || return 2
    # Add code here, if necessary, that waits for the process to be ready
    # to handle requests from services started subsequently which depend
    # on this one.  As a last resort, sleep for some time.
    [ "$OPEN_SOCKET" != no ] && sleep 3 && sh -c "chmod 0777 /tmp/god.*.sock;true"
}

# kills god + everything god is monitoring
do_terminate()
{
    $DAEMON terminate
    RETVAL="$?"
    return "$RETVAL"
}

#
# Function that stops the daemon/service
#
do_stop()
{
    $DAEMON quit
    RETVAL="$?"
    return "$RETVAL"
}

#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
    #
    # If the daemon can reload its configuration without
    # restarting (for example, when it is sent a SIGHUP),
    # then implement that here.
    #
    log_daemon_msg "Reloading $DESC" "$NAME"
    $DAEMON load $CONF
    return 0
}

case "$1" in
  start)
    [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
    do_start
    case "$?" in
        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
    esac
    ;;
  stop)
    if [ "$0" != "$SCRIPTNAME" ] && [ "$USE_TERMINATE_ON_KILL" = "yes" ]; then
      [ "$VERBOSE" != no ] && log_daemon_msg "Terminating $DESC" "$NAME"
      do_terminate
    else
      [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
      do_stop
    fi
    case "$?" in
        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
    esac
    ;;
  terminate)
    [ "$VERBOSE" != no ] && log_daemon_msg "Terminating $DESC" "$NAME"
    do_terminate
    case "$?" in
            0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
            2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
    esac
    ;;
  status)
    $DAEMON status && exit 0 || exit $?
    status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
    ;;
  reload|force-reload)
    do_reload
    log_end_msg $?
    ;;
  restart)
    #
    # If the "reload" option is implemented then remove the
    # 'force-reload' alias
    #
    log_daemon_msg "Restarting $DESC" "$NAME"
    do_stop
    case "$?" in
      0|1)
        do_start
        case "$?" in
            0) log_end_msg 0 ;;
            1) log_end_msg 1 ;; # Old process is still running
            *) log_end_msg 1 ;; # Failed to start
        esac
        ;;
      *)
        # Failed to stop
        log_end_msg 1
        ;;
    esac
    ;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop|status|restart|terminate|force-reload}" >&2
    exit 3
    ;;
esac

相关内容