如何在 Linux 中将 Perl 脚本作为系统守护程序运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/766397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:13:04  来源:igfitidea点击:

How can I run a Perl script as a system daemon in linux?

linuxperlstartupdaemonshutdown

提问by jedihawk

What's a simple way to get a Perl script to run as a daemon in linux?

让 Perl 脚本在 linux 中作为守护程序运行的简单方法是什么?

Currently, this is on CentOS. I'd want it to start up with the system and shutdown with the system, so some /etc/rc.d/init.dintegration would also be nice, but I could always add a custom line to /etc/rc.d/rc.local.

目前,这是在 CentOS 上。我希望它与系统一起启动并与系统一起关闭,所以一些/etc/rc.d/init.d集成也很好,但我总是可以在/etc/rc.d/rc.local.

采纳答案by Chas. Owens

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 所做的所有事情:

  1. Fork a child and exits the parent process.
  2. Become a session leader (which detaches the program from the controlling terminal).
  3. Fork another child process and exit first child. This prevents the potential of acquiring a controlling terminal.
  4. Change the current working directory to "/".
  5. Clear the file creation mask.
  6. Close all open file descriptors.
  1. Fork 一个子进程并退出父进程。
  2. 成为会话负责人(将程序与控制终端分离)。
  3. Fork 另一个子进程并退出第一个子进程。这防止了获得控制终端的可能性。
  4. 将当前工作目录更改为"/".
  5. 清除文件创建掩码。
  6. 关闭所有打开的文件描述符。

Integrating with the runlevel system is easy. You need a script like the following (replace XXXXXXXXXXXXwith the Perl script's name, YYYYYYYYYYYYYYYYYYYwith a description of what it does, and /path/towith path to the Perl script) in /etc/init.d. 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.

与运行级别系统集成很容易。你需要像下面的脚本(替换XXXXXXXXXXXX用Perl脚本的名称,YYYYYYYYYYYYYYYYYYY用它做什么的说明,并/path/to与路径Perl脚本)的/etc/init.d。由于您使用的是 CentOS,因此一旦您/etc/init.d在 .

#!/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 "" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    status)
        status XXXXXXXXXXXX
    ;;
    restart)
        restart
    ;;
    *)
        echo "Usage: 
daemon myscript args
{start|stop|status|restart}" ;; esac exit 0

回答by Zifre

I think the easiest way is to use 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. To use it, just use:

我认为最简单的方法是使用daemon。它允许您将任何进程作为守护进程运行。这意味着,例如,如果您决定改用 Python,则不必担心库。要使用它,只需使用:

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");
 }

This should be available on most distros, but it might not be installed by default.

这应该在大多数发行版上可用,但默认情况下可能不会安装。

回答by Bklyn

If you don't have Proc::Daemonas suggested by Chas. Owens, here's how you'd do it by hand:

如果您没有Chas 建议的Proc::Daemon。Owens,这是您手动完成的方法:

##代码##