[英]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: 无论如何,解决方案将涉及以下任一方面:
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.