- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- perl daemon script
Operating System - HP-UX
1754324
Members
2585
Online
108813
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-28-2009 06:48 AM
тАО07-28-2009 06:48 AM
Plese help me with Perl daemon script.
When I'm executing the main script, it created child process,but don't want to exit itself. :(
I have to press enter, in oder to stop the main script. I want to use this script in
/sbin/init.d, so I want the main script to exit itself, and the child process to remain as daemon, doing some job...
Here is the script bellow:
#!/usr/bin/perl -w
use POSIX;
use Getopt::Std;
use English;
use strict;
use constant FALSE => 0;
use constant TRUE => 1;
# ****** Main Program
my $kontinue = TRUE;
sub close_n_exit
{
$kontinue = FALSE;
return(15);
} # close_n_exit
my $cc = 0;
my $pid = fork();
if ($pid == 0)
{ # child - daemonize the process
POSIX::setsid() or die "Can't start a new session: $!";
$SIG{HUP} = 'IGNORE';
# $SIG{QUIT} = 'IGNORE';
$SIG{INT} = 'IGNORE';
$SIG{PIPE} = 'IGNORE';
$SIG{TERM} = \&close_n_exit;
while ($kontinue)
{
print `date`;
sleep(10);
}
}
else
{ # parent process - just exit; the child is doing the real work
exit($cc);
}
exit($cc);
Please help me to make the right script.
It will be points, of course :)
When I'm executing the main script, it created child process,but don't want to exit itself. :(
I have to press enter, in oder to stop the main script. I want to use this script in
/sbin/init.d, so I want the main script to exit itself, and the child process to remain as daemon, doing some job...
Here is the script bellow:
#!/usr/bin/perl -w
use POSIX;
use Getopt::Std;
use English;
use strict;
use constant FALSE => 0;
use constant TRUE => 1;
# ****** Main Program
my $kontinue = TRUE;
sub close_n_exit
{
$kontinue = FALSE;
return(15);
} # close_n_exit
my $cc = 0;
my $pid = fork();
if ($pid == 0)
{ # child - daemonize the process
POSIX::setsid() or die "Can't start a new session: $!";
$SIG{HUP} = 'IGNORE';
# $SIG{QUIT} = 'IGNORE';
$SIG{INT} = 'IGNORE';
$SIG{PIPE} = 'IGNORE';
$SIG{TERM} = \&close_n_exit;
while ($kontinue)
{
print `date`;
sleep(10);
}
}
else
{ # parent process - just exit; the child is doing the real work
exit($cc);
}
exit($cc);
Please help me to make the right script.
It will be points, of course :)
Solved! Go to Solution.
- Tags:
- Perl
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-28-2009 07:42 AM
тАО07-28-2009 07:42 AM
Solution
Hi:
Run this version. Notice that the child process directs its STDOUT to a file (which you can view with 'tail -f' as the daemon runs on).
Your code is decidedly C-like. A much more Perl-ish version is this:
#!/usr/bin/perl
use POSIX qw( setsid );
use strict;
use warnings;
my $logfile = '/var/tmp/mydaemon';
my $pid;
sub close_n_exit {
print STDERR "going away...\n";
exit 1;
} # close_n_exit
chdir '/' or die "Can't cd to /: $!\n";
open( STDIN , '<', '/dev/null' ) or die "Can't open /dev/null: $!\n";
open( STDOUT, '>', $logfile ) or die "Can't open '$logfile': $!\n";
$|++; #...set autoflush on STDOUT...
defined($pid = fork) or die "Can't fork(): $!\n";
exit if $pid;
setsid or die "Can't start a new session: $!";
$SIG{HUP} = 'IGNORE';
$SIG{QUIT} = 'IGNORE';
$SIG{INT} = 'IGNORE';
$SIG{PIPE} = 'IGNORE';
$SIG{TERM} = \&close_n_exit;
while (1) {
print STDOUT scalar localtime, "\n";
sleep 10;
}
exit 0
Regards!
...JRF...
Run this version. Notice that the child process directs its STDOUT to a file (which you can view with 'tail -f' as the daemon runs on).
Your code is decidedly C-like. A much more Perl-ish version is this:
#!/usr/bin/perl
use POSIX qw( setsid );
use strict;
use warnings;
my $logfile = '/var/tmp/mydaemon';
my $pid;
sub close_n_exit {
print STDERR "going away...\n";
exit 1;
} # close_n_exit
chdir '/' or die "Can't cd to /: $!\n";
open( STDIN , '<', '/dev/null' ) or die "Can't open /dev/null: $!\n";
open( STDOUT, '>', $logfile ) or die "Can't open '$logfile': $!\n";
$|++; #...set autoflush on STDOUT...
defined($pid = fork) or die "Can't fork(): $!\n";
exit if $pid;
setsid or die "Can't start a new session: $!";
$SIG{HUP} = 'IGNORE';
$SIG{QUIT} = 'IGNORE';
$SIG{INT} = 'IGNORE';
$SIG{PIPE} = 'IGNORE';
$SIG{TERM} = \&close_n_exit;
while (1) {
print STDOUT scalar localtime, "\n";
sleep 10;
}
exit 0
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-28-2009 10:36 PM
тАО07-28-2009 10:36 PM
Re: perl daemon script
Thanks ,James
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP