When looking at various daemon scripts in /etc/init.d/, I can't seem to understand the purpose of the 'lockfile' variable. It seems like the 'lockfile' variable is not being checked before starting the daemon.
For example, some code from /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?
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. (ie replacing the 'daemon $prog' in the code above with a call to my binary?)
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 ; 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...
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. In any case, solutions will involve either:
stat
) could be used but there's no way to wait on it without repeated polling . 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).
rc脚本跟踪它是否正在运行,并且不必费心停止未运行的内容。
What is the 'lockfile' variable doing?
It could be nothing or it could be eg. injected into $OPTIONS
by this line
. /etc/sysconfig/ntpd
The daemon takes the option -p pidfile
where $lockfile
could go. The daemon writes its $PID
in this file.
do I put the compiled binary directly in /etc/init.d/ or do I put a script there that calls the binary
The latter. There should be no binaries in /etc
, and its customary to edit /etc/init.d
scripts for configuration changes. Binaries should go to /(s)bin
or /usr/(s)bin
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.