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

时间:2022-11-08 01:08:11

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.d integration 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. dc /rc. local中添加自定义行。

3 个解决方案

#1


81  

The easiest way is to use Proc::Daemon.

最简单的方法是使用Proc::守护进程。

#!/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::守护进程所做的所有事情:

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

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. 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.

与运行级系统集成很容易。您需要使用/etc/init.d中的脚本(将XXXXXXXXXXXX替换为Perl脚本的名称yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,并使用它所做的事情的描述以及/to和/to的Perl脚本的路径)))由于您使用的是CentOS,所以一旦您有了/etc/ initc中的脚本。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

#2


38  

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

如果您没有Chas建议的Proc::守护进程。欧文斯,这是你手工操作的方法:

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

#3


7  

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:

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

daemon myscript args

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

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

#1


81  

The easiest way is to use Proc::Daemon.

最简单的方法是使用Proc::守护进程。

#!/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::守护进程所做的所有事情:

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

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. 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.

与运行级系统集成很容易。您需要使用/etc/init.d中的脚本(将XXXXXXXXXXXX替换为Perl脚本的名称yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,并使用它所做的事情的描述以及/to和/to的Perl脚本的路径)))由于您使用的是CentOS,所以一旦您有了/etc/ initc中的脚本。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

#2


38  

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

如果您没有Chas建议的Proc::守护进程。欧文斯,这是你手工操作的方法:

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

#3


7  

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:

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

daemon myscript args

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

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