[英]How can I run a Perl script as a system daemon in linux?
What's a simple way to get a Perl script to run as a daemon in linux?让 Perl 脚本在 linux 中作为守护程序运行的简单方法是什么?
Currently, this is on CentOS.目前,这是在 CentOS 上。 I'd want it to start up with the system and shutdown with the system, so some
/etc/rc.d/init.d
integration would also be nice, but I could always add a custom line to /etc/rc.d/rc.local
.我希望它与系统一起启动并与系统一起关闭,所以一些
/etc/rc.d/init.d
集成也很好,但我总是可以向/etc/rc.d/rc.local
添加自定义行/etc/rc.d/rc.local
。
The easiest way is to use Proc::Daemon .最简单的方法是使用Proc::Daemon 。
#!/usr/bin/perl
use strict;
use warnings;
use Proc::Daemon;
Proc::Daemon::Init;
my $continue = 1;
$SIG{TERM} = sub { $continue = 0 };
while ($continue) {
#do stuff
}
Alternately you could do all of the things Proc::Daemon does:或者,您可以执行 Proc::Daemon 所做的所有事情:
"/"
."/"
。 Integrating with the runlevel system is easy.与运行级别系统集成很容易。 You need a script like the following (replace
XXXXXXXXXXXX
with the Perl script's name, YYYYYYYYYYYYYYYYYYY
with a description of what it does, and /path/to
with path to the Perl script) in /etc/init.d
.在
/etc/init.d
中,您需要一个类似以下的脚本(用 Perl 脚本的名称替换XXXXXXXXXXXX
,用它的作用描述替换YYYYYYYYYYYYYYYYYYY
用 Perl 脚本的/path/to
替换/path/to
)。 Since you are using CentOS, once you have the script in /etc/init.d
, you can just use chkconfig to turn it off or on in the various runlevels.由于您使用的是 CentOS,一旦您在
/etc/init.d
有脚本,您就可以使用 chkconfig 在各种运行级别中关闭或打开它。
#!/bin/bash
#
# XXXXXXXXXXXX This starts and stops XXXXXXXXXXXX
#
# chkconfig: 2345 12 88
# description: XXXXXXXXXXXX is YYYYYYYYYYYYYYYYYYY
# processname: XXXXXXXXXXXX
# pidfile: /var/run/XXXXXXXXXXXX.pid
### BEGIN INIT INFO
# Provides: $XXXXXXXXXXXX
### END INIT INFO
# Source function library.
. /etc/init.d/functions
binary="/path/to/XXXXXXXXXXXX"
[ -x $binary ] || exit 0
RETVAL=0
start() {
echo -n "Starting XXXXXXXXXXXX: "
daemon $binary
RETVAL=$?
PID=$!
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/XXXXXXXXXXXX
echo $PID > /var/run/XXXXXXXXXXXX.pid
}
stop() {
echo -n "Shutting down XXXXXXXXXXXX: "
killproc XXXXXXXXXXXX
RETVAL=$?
echo
if [ $RETVAL -eq 0 ]; then
rm -f /var/lock/subsys/XXXXXXXXXXXX
rm -f /var/run/XXXXXXXXXXXX.pid
fi
}
restart() {
echo -n "Restarting XXXXXXXXXXXX: "
stop
sleep 2
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status XXXXXXXXXXXX
;;
restart)
restart
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
;;
esac
exit 0
If you don't have Proc::Daemon as suggested by Chas.如果您没有 Chas 建议的Proc::Daemon 。 Owens, here's how you'd do it by hand:
Owens,这是您手动完成的方法:
sub daemonize {
use POSIX;
POSIX::setsid or die "setsid: $!";
my $pid = fork() // die $!; #//
exit(0) if $pid;
chdir "/";
umask 0;
for (0 .. (POSIX::sysconf (&POSIX::_SC_OPEN_MAX) || 1024))
{ POSIX::close $_ }
open (STDIN, "</dev/null");
open (STDOUT, ">/dev/null");
open (STDERR, ">&STDOUT");
}
I think the easiest way is to use daemon .我认为最简单的方法是使用daemon 。 It allows you to run any process as a daemon.
它允许您将任何进程作为守护进程运行。 This means you don't have to worry about libraries if you, for example, decided to change to python.
这意味着,例如,如果您决定改用 Python,则不必担心库。 To use it, just use:
要使用它,只需使用:
daemon myscript args
This should be available on most distros, but it might not be installed by default.这应该在大多数发行版上可用,但默认情况下可能不会安装。
I used supervisor for running a perl script.我使用 supervisor 来运行 perl 脚本。
As a system administrator, I like to minimise changes and variations among server and like to stick to core services or bare minimum.作为系统管理员,我喜欢尽量减少服务器之间的变化和变化,喜欢坚持核心服务或最低限度。
Supervisor was already installed and available for a python-flask application running on the same box. Supervisor 已经安装并且可用于在同一台机器上运行的 python-flask 应用程序。 So, I just added a conf file for the perl script I wanted to run as a service.
所以,我只是为我想作为服务运行的 perl 脚本添加了一个 conf 文件。 Now, I can do
现在,我可以
supervisorctl start/stop/restart my_perl_script_supervisor_service_name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.