简体   繁体   English

init.d守护程序脚本中的lockfile用途(Linux)

[英]lockfile purpose in init.d daemon scripts (linux)

When looking at various daemon scripts in /etc/init.d/, I can't seem to understand the purpose of the 'lockfile' variable. 在/etc/init.d/中查看各种守护程序脚本时,我似乎无法理解'lockfile'变量的用途。 It seems like the 'lockfile' variable is not being checked before starting the daemon. 似乎在启动守护程序之前未检查'lockfile'变量。

For example, some code from /etc/init.d/ntpd: 例如,/etc/init.d/ntpd中的一些代码:

prog=ntpd
lockfile=/var/lock/subsys/$prog

start() {
        [ "$EUID" != "0" ] && exit 4
        [ "$NETWORKING" = "no" ] && exit 1
        [ -x /usr/sbin/ntpd ] || exit 5
        [ -f /etc/sysconfig/ntpd ] || exit 6
        . /etc/sysconfig/ntpd

        # Start daemons.
        echo -n $"Starting $prog: "
        daemon $prog $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch $lockfile
        return $RETVAL
}

What is the 'lockfile' variable doing? 'lockfile'变量在做什么?

Also, when writing my own daemon in C++ (such as following the example at the bottom of http://www.itp.uzh.ch/~dpotter/howto/daemonize ), do I put the compiled binary directly in /etc/init.d/ or do I put a script there that calls the binary. 另外,在用C ++编写自己的守护程序时(例如按照http://www.itp.uzh.ch/~dpotter/howto/daemonize底部的示例),我是否将编译后的二进制文件直接放在/ etc /中。 init.d /还是在其中放置一个调用二进制文件的脚本。 (ie replacing the 'daemon $prog' in the code above with a call to my binary?) (即用对我的二进制文件的调用替换上面代码中的“ daemon $ prog”?)

The whole thing is a very fragile and misguided attempt to keep track of whether a given daemon is running in order to know whether/how to shut it down later. 整个过程是非常脆弱且被误导的尝试,目的是跟踪给定的守护程序是否正在运行,以便知道以后是否/如何将其关闭。 Using pids does not help, because a pid is meaningless to any process except the direct parent of a process ; 使用pid并没有帮助,因为pid 对于除进程的直接父代之外的任何进程都没有意义 any other use has unsolvable and dangerous race conditions (ie you could end up killing another unrelated process). 任何其他用途都具有无法解决且危险的比赛条件(即您可能最终杀死另一个不相关的过程)。 Unfortunately, this kind of ill-designed (or rather undesigned) hackery is standard practice on most unix systems... 不幸的是,这种设计不当(或未设计)的黑客是大多数Unix系统上的标准做法。

There are a couple approaches to solving the problem correctly. 有两种方法可以正确解决问题。 One is the systemd approach, but systemd is disliked among some circles for being "bloated" and for making it difficult to use a remote /usr mounted after initial boot. 一种是systemd方法,但是在某些圈子中, systemd不受欢迎,因为它会“ blo肿”,并且在初始引导后很难使用远程/usr挂载。 In any case, solutions will involve either: 无论如何,解决方案将涉及以下任一方面:

  1. Use of a master process that spawns all daemons as direct children (ie inhibiting "daemonizing" within the individual daemons) and which thereby can use their pids to watch for them exiting, keep track of their status, and kill them as desired. 使用主进程将所有守护程序作为直接子代生成(即禁止单个守护程序内的“守护进程”),从而可以使用它们的pid监视它们的退出,跟踪其状态并根据需要杀死它们。
  2. Arranging for every daemon to inherit an otherwise-useless file descriptor, which it will keep open and atomically close only as part of process termination. 安排每个守护程序继承一个本来没有用的文件描述符,该文件描述符将仅在进程终止的过程中保持打开和原子关闭。 Pipes (anonymous or named fifos), sockets, or even ordinary files are all possibilities, but file types which give EOF as soon as the "other end" is closed are the most suitable, since it's possible to block waiting for this status. 管道(匿名或命名为fifos),套接字甚至普通文件都是可能的,但是最合适的是在“另一端”关闭时立即提供EOF的文件类型,因为可以阻止等待此状态。 With ordinary files, the link count (from stat ) could be used but there's no way to wait on it without repeated polling . 对于普通文件,可以使用链接数(来自stat ),但是没有重复轮询就无法等待。

In any case, the lockfile/pidfile approach is ugly, error-prone, and hardly any better than lazy approaches like simply killall foobard (which of course are also wrong). 在任何情况下,lockfile / pidfile方法都是丑陋的,容易出错的,并且几乎没有比像killall foobard这样的惰性方法更好的方法(当然这也是错误的)。

rc脚本跟踪它是否正在运行,并且不必费心停止未运行的内容。

What is the 'lockfile' variable doing? 'lockfile'变量在做什么?

It could be nothing or it could be eg. 它可能什么也不是,例如。 injected into $OPTIONS by this line 通过此行注入到$OPTIONS

   . /etc/sysconfig/ntpd

The daemon takes the option -p pidfile where $lockfile could go. 守护程序将使用-p pidfile选项,其中$lockfile可以进入。 The daemon writes its $PID in this file. 守护程序将其$PID写入此文件。

do I put the compiled binary directly in /etc/init.d/ or do I put a script there that calls the binary 我将编译后的二进制文件直接放在/etc/init.d/中还是在其中放置了一个调用二进制文件的脚本

The latter. 后者。 There should be no binaries in /etc , and its customary to edit /etc/init.d scripts for configuration changes. /etc应该没有二进制文件,并且习惯上要编辑/etc/init.d脚本以进行配置更改。 Binaries should go to /(s)bin or /usr/(s)bin . 二进制文件应该转到/(s)bin/usr/(s)bin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM