Reputation: 275
I am running a shell script from a perl CGI script:
#!/usr/bin/perl
my $command = "./script.sh &";
my $pid = fork();
if (defined($pid) && $pid==0) {
# background process
system( $command );
}
The shell script looks like this:
#!/bin/sh
trap 'echo trapped' 15
tail -f test.log
When I run the CGI script from browser, and then stop httpd using /etc/init.d/httpd stop
, the script receives a SIGTERM signal.
I was expecting the script to run as a separate process and not be tied in anyway to httpd. Though I can trap the SIGTERM, I would like to understand why the script is receiving SIGTERM at all.
What wrong am I doing here? I am running RHEL 5.8 and Apache HTTP server 2.4.
Thanks, Pranav
Upvotes: 1
Views: 348
Reputation: 47909
The process you are spawning still has httpd's parent PID attached to it. So this might work:
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;
}
And since it doesn't sound like your process should ever return back to your script, you might even do
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 );
}
Upvotes: 1