【问题标题】:Stopping httpd causes a process started from perl CGI script to receive SIGTERM停止 httpd 会导致从 perl CGI 脚本启动的进程接收 SIGTERM
【发布时间】:2013-06-28 06:43:12
【问题描述】:

我正在从 perl CGI 脚本运行一个 shell 脚本:

#!/usr/bin/perl
my $command = "./script.sh &";
my $pid = fork();
if (defined($pid) && $pid==0) {
        # background process
        system( $command );
}

shell 脚本如下所示:

#!/bin/sh
trap 'echo trapped' 15
tail -f test.log

当我从浏览器运行 CGI 脚本,然后使用 /etc/init.d/httpd stop 停止 httpd 时,脚本会收到 SIGTERM 信号。

我希望脚本作为一个单独的进程运行,而不是与 httpd 绑定。虽然我可以捕获 SIGTERM,但我想了解脚本为什么会收到 SIGTERM。

我在这里做错了什么?我正在运行 RHEL 5.8 和 Apache HTTP 服务器 2.4。

谢谢, 普拉纳夫

【问题讨论】:

  • 你打算用这个实现什么?也许是更好的方法。
  • 好吧,我写的脚本只是一个虚拟脚本。实际脚本应该运行一个相当长的进程,作为该进程的一部分,它需要关闭 httpd。

标签: perl apache unix cgi signals


【解决方案1】:

您正在生成的进程仍然附加了 httpd 的父 PID。所以这可能有效:

use POSIX qw / setsid /;
...
my $command = "./script.sh";
my $pid = fork();
if (defined $pid && $pid == 0) {
    close *STDIN;
    close *STDOUT;
    close *STDERR;
    setsid;
    system( $command );
    exit 0;
}

而且由于听起来您的流程不应该返回到您的脚本,您甚至可以这样做

use POSIX qw / setsid /;
...
my $command = "./script.sh";
my $pid = fork();
if (defined $pid && $pid == 0) {
    close *STDIN;
    close *STDOUT;
    close *STDERR;
    setsid;
    exec( $command );
}

【讨论】:

    猜你喜欢
    • 2018-01-09
    • 1970-01-01
    • 2021-02-20
    • 2012-11-23
    • 2023-03-12
    • 2014-07-02
    • 2018-02-08
    • 1970-01-01
    • 2012-03-10
    相关资源
    最近更新 更多