【问题标题】:Service that uses libcurl not auto-starting on Debian?使用 libcurl 的服务不会在 Debian 上自动启动?
【发布时间】:2017-08-26 07:24:09
【问题描述】:

我已经用 C 语言编写并编译了一个守护程序,该程序旨在以 root 访问权限在后台运行。我的程序使用 libcurl 进行一些偶尔的网络调用。我还编写了一个简单的 init.d 脚本来管理它的启动和关闭过程。我希望这项服务在启动时自动启动,并且根据我所做的,我希望它已经这样做了。但是,我注意到与 libcurl 相关的日志中有错误,因此服务没有自动启动。

我的程序位于/usr/bin/myprog,我在/etc/init.d/myprog 有以下 bash 脚本:

#!/bin/bash
### BEGIN INIT INFO
# Provides: myprog
# Required-Start: $remote_fs $network $syslog
# Required-Stop: $remote_fs $network $syslog
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: myprog
# Description: My Daemon Program
### END INIT INFO

. /lib/lsb/init-functions

SCRIPT=/usr/bin/myprog
PIDFILE=/var/run/myprog.pid

start() {
    if [ -f $PIDFILE ]; then
        echo "Service is already started"
        return 2
    else
        $SCRIPT
        $RETVAL="$?"
        return "${RETVAL}"
    fi
}

stop() {
    if [ -f $PIDFILE ]; then
        kill $(cat $PIDFILE)
        rm -f $PIDFILE
        return 0
    else
        echo "Service is not running"
        return 2
    fi
}

case "$1" in
    start)
        log_daemon_msg "Starting myprog" "myprog"
        start
        ;;
    stop)
        log_daemon_msg "Stopping myprog" "myprog"
        stop
        ;;
    status)
        status_of_proce "$SCRIPT" "myprog" && exit 0 || exit $?
        ;;
    restart)
        log_daemon_msg "Restarting myprog" "myprog"
        stop
        start
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart}" >&2
        exit 3
        ;;
esac

然后我运行 sudo update-rc.d myprog defaults 并创建了以下文件:

  • /etc/rc0.d/K01myprog
  • /etc/rc1.d/K01myprog
  • /etc/rc2.d/K01myprog
  • /etc/rc3.d/S02myprog
  • /etc/rc4.d/S02myprog
  • /etc/rc5.d/S02myprog
  • /etc/rc6.d/K01myprog

据我所知,这 7 个文件中的每一个都是我在上面发布的文件的相同副本。根据我一直在阅读的各种教程和论坛,我认为这已经足够了。但是,我的服务似乎没有在启动时自动启动。如果我直接打电话给sudo /etc/init.d/myprog start,那么它可以正常启动。但否则它似乎没有启动。

然后我注意到日志中有一条错误消息,上面写着“curl 错误:无法解析主机”,即使它引用的特定主机肯定是有效的。所以我认为它可能试图在 libcurl 需要的东西准备好之前启动我的应用程序,因此无法启动。同样,如果我手动启动它,它工作正常。我该如何解决这个问题?

【问题讨论】:

  • 您应该使用dmesg|grep "curl" 并发布实际错误。
  • 消息是“Could not resolve host: www.xxxxxx.com” 这里我正在审查实际的域,因为它是为了工作而我不想分享 URL,但足够了说 URL 绝对是有效的。我可以手动运行相同的程序,然后它就可以工作,这进一步证实了这一点。
  • 该消息也来自我自己的日志(因为我正在设置 CURLOPT_ERRORBUFFER 并登录失败);当我运行 dmesg 命令时,您建议它只是空白。

标签: linux bash curl debian init.d


【解决方案1】:

如果 DNS 解析是故障点,请改用 IP 地址。如果您不愿意使用 IP 地址,请将主机名和 IP 地址添加到 /etc/hosts。

【讨论】:

  • 但我不认为 DNS 解析是真正的故障点,因为我可以手动运行相同的代码,然后它就可以正常工作。似乎它试图过早地运行它,而底层系统还没有为 DNS 解析(或其他任何东西)做好准备。
  • 很容易尝试我的建议并确定。
  • 当您认为 DNS 解析为 Amazon 服务器并因此 IP 可能会定期更改时,这在商业环境中是不切实际的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多