在/etc/init.d脚本中对守护程序的调用被阻止,未在后台运行 [英] Call to daemon in a /etc/init.d script is blocking, not running in background
问题描述
我有一个要守护的Perl脚本.基本上,此perl脚本将每30秒读取一个目录,读取其找到的文件,然后处理数据.为了简单起见,请考虑以下Perl脚本(称为synpipe_server,该脚本在/usr/sbin/
中有符号链接):
I have a Perl script that I want to daemonize. Basically this perl script will read a directory every 30 seconds, read the files that it finds and then process the data. To keep it simple here consider the following Perl script (called synpipe_server, there is a symbolic link of this script in /usr/sbin/
) :
#!/usr/bin/perl
use strict;
use warnings;
my $continue = 1;
$SIG{'TERM'} = sub { $continue = 0; print "Caught TERM signal\n"; };
$SIG{'INT'} = sub { $continue = 0; print "Caught INT signal\n"; };
my $i = 0;
while ($continue) {
#do stuff
print "Hello, I am running " . ++$i . "\n";
sleep 3;
}
因此,此脚本基本上每3秒打印一次内容.
So this script basically prints something every 3 seconds.
然后,因为我想守护该脚本,所以我也将此bash脚本(也称为synpipe_server)放在/etc/init.d/
中:
Then, as I want to daemonize this script, I've also put this bash script (also called synpipe_server) in /etc/init.d/
:
#!/bin/bash
# synpipe_server : This starts and stops synpipe_server
#
# chkconfig: 12345 12 88
# description: Monitors all production pipelines
# processname: synpipe_server
# pidfile: /var/run/synpipe_server.pid
# Source function library.
. /etc/rc.d/init.d/functions
pname="synpipe_server"
exe="/usr/sbin/synpipe_server"
pidfile="/var/run/${pname}.pid"
lockfile="/var/lock/subsys/${pname}"
[ -x $exe ] || exit 0
RETVAL=0
start() {
echo -n "Starting $pname : "
daemon ${exe}
RETVAL=$?
PID=$!
echo
[ $RETVAL -eq 0 ] && touch ${lockfile}
echo $PID > ${pidfile}
}
stop() {
echo -n "Shutting down $pname : "
killproc ${exe}
RETVAL=$?
echo
if [ $RETVAL -eq 0 ]; then
rm -f ${lockfile}
rm -f ${pidfile}
fi
}
restart() {
echo -n "Restarting $pname : "
stop
sleep 2
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status ${pname}
;;
restart)
restart
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
;; esac
exit 0
因此,(如果我很了解守护程序的文档)Perl脚本应该在后台运行,并且如果执行,输出应该重定向到/dev/null
.
So, (if I have well understood the doc for daemon) the Perl script should run in the background and the output should be redirected to /dev/null
if I execute :
service synpipe_server start
但这是我得到的:
[root@master init.d]# service synpipe_server start
Starting synpipe_server : Hello, I am running 1
Hello, I am running 2
Hello, I am running 3
Hello, I am running 4
Caught INT signal
[ OK ]
[root@master init.d]#
因此,它启动了Perl脚本,但是运行了它,而没有将其与当前的终端会话分离,因此我可以在控制台中看到输出的输出……这并不是我真正期望的.此外,PID文件为空(或仅带有换行符,守护程序没有返回pid).
So it starts the Perl script but runs it without detaching it from the current terminal session, and I can see the output printed in my console ... which is not really what I was expecting. Moreover, the PID file is empty (or with a line feed only, no pid returned by daemon).
有人知道我在做什么错吗?
Does anyone have any idea of what I am doing wrong ?
也许我应该说我在Red Hat机器上.
EDIT : maybe I should say that I am on a Red Hat machine.
Scientific Linux SL release 5.4 (Boron)
谢谢, 托尼
推荐答案
我终于在bash初始化脚本中重新编写了start函数,并且我不再使用daemon
.
I finally re-wrote the start function in the bash init script, and I am not using daemon
anymore.
start() {
echo -n "Starting $pname : "
#daemon ${exe} # Not working ...
if [ -s ${pidfile} ]; then
RETVAL=1
echo -n "Already running !" && warning
echo
else
nohup ${exe} >/dev/null 2>&1 &
RETVAL=$?
PID=$!
[ $RETVAL -eq 0 ] && touch ${lockfile} && success || failure
echo
echo $PID > ${pidfile}
fi
}
我检查pid文件是否不存在(如果存在,请写警告).如果没有,我使用
I check that the pid file is not existing already (if so, just write a warning). If not, I use
nohup ${exe} >/dev/null 2>&1 &
启动脚本.
我不知道这种方法是否安全(?),但可以使用.
I don't know if it is safe this way (?) but it works.
这篇关于在/etc/init.d脚本中对守护程序的调用被阻止,未在后台运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!