<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic perl daemon script in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/perl-daemon-script/m-p/4468288#M680123</link>
    <description>Plese help me with Perl daemon script.&lt;BR /&gt;When I'm executing the main script, it created child process,but don't want to exit itself. :(&lt;BR /&gt;I have to press enter, in oder to stop the main script. I want to use this script in &lt;BR /&gt;/sbin/init.d, so I want the main script to exit itself, and the child process to remain as daemon, doing some job...&lt;BR /&gt;Here is the script bellow:&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/perl -w&lt;BR /&gt;&lt;BR /&gt;use POSIX;&lt;BR /&gt;use Getopt::Std;&lt;BR /&gt;use English;&lt;BR /&gt;use strict;&lt;BR /&gt;&lt;BR /&gt;use constant FALSE              =&amp;gt; 0;&lt;BR /&gt;use constant TRUE               =&amp;gt; 1;&lt;BR /&gt;&lt;BR /&gt;# ****** Main Program &lt;BR /&gt;&lt;BR /&gt;my $kontinue = TRUE;&lt;BR /&gt;&lt;BR /&gt;sub close_n_exit&lt;BR /&gt;{&lt;BR /&gt;  $kontinue = FALSE;&lt;BR /&gt;  return(15);&lt;BR /&gt;} # close_n_exit&lt;BR /&gt;&lt;BR /&gt;my $cc = 0;&lt;BR /&gt;my $pid = fork();&lt;BR /&gt;if ($pid == 0)&lt;BR /&gt;  { # child - daemonize the process&lt;BR /&gt;&lt;BR /&gt;    POSIX::setsid() or die "Can't start a new session: $!";&lt;BR /&gt;    $SIG{HUP} = 'IGNORE';&lt;BR /&gt;#    $SIG{QUIT} = 'IGNORE';&lt;BR /&gt;    $SIG{INT} = 'IGNORE';&lt;BR /&gt;    $SIG{PIPE} = 'IGNORE';&lt;BR /&gt;    $SIG{TERM} = \&amp;amp;close_n_exit;&lt;BR /&gt;    while ($kontinue)&lt;BR /&gt;      {&lt;BR /&gt;        print `date`;&lt;BR /&gt;        sleep(10);&lt;BR /&gt;      }&lt;BR /&gt;  }&lt;BR /&gt;else&lt;BR /&gt;  { # parent process - just exit; the child is doing the real work&lt;BR /&gt;    exit($cc);&lt;BR /&gt;  }&lt;BR /&gt;exit($cc);&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Please help me to make the right script.&lt;BR /&gt;It will be points, of course :)</description>
    <pubDate>Tue, 28 Jul 2009 13:48:05 GMT</pubDate>
    <dc:creator>Inesa Clinko</dc:creator>
    <dc:date>2009-07-28T13:48:05Z</dc:date>
    <item>
      <title>perl daemon script</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/perl-daemon-script/m-p/4468288#M680123</link>
      <description>Plese help me with Perl daemon script.&lt;BR /&gt;When I'm executing the main script, it created child process,but don't want to exit itself. :(&lt;BR /&gt;I have to press enter, in oder to stop the main script. I want to use this script in &lt;BR /&gt;/sbin/init.d, so I want the main script to exit itself, and the child process to remain as daemon, doing some job...&lt;BR /&gt;Here is the script bellow:&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/perl -w&lt;BR /&gt;&lt;BR /&gt;use POSIX;&lt;BR /&gt;use Getopt::Std;&lt;BR /&gt;use English;&lt;BR /&gt;use strict;&lt;BR /&gt;&lt;BR /&gt;use constant FALSE              =&amp;gt; 0;&lt;BR /&gt;use constant TRUE               =&amp;gt; 1;&lt;BR /&gt;&lt;BR /&gt;# ****** Main Program &lt;BR /&gt;&lt;BR /&gt;my $kontinue = TRUE;&lt;BR /&gt;&lt;BR /&gt;sub close_n_exit&lt;BR /&gt;{&lt;BR /&gt;  $kontinue = FALSE;&lt;BR /&gt;  return(15);&lt;BR /&gt;} # close_n_exit&lt;BR /&gt;&lt;BR /&gt;my $cc = 0;&lt;BR /&gt;my $pid = fork();&lt;BR /&gt;if ($pid == 0)&lt;BR /&gt;  { # child - daemonize the process&lt;BR /&gt;&lt;BR /&gt;    POSIX::setsid() or die "Can't start a new session: $!";&lt;BR /&gt;    $SIG{HUP} = 'IGNORE';&lt;BR /&gt;#    $SIG{QUIT} = 'IGNORE';&lt;BR /&gt;    $SIG{INT} = 'IGNORE';&lt;BR /&gt;    $SIG{PIPE} = 'IGNORE';&lt;BR /&gt;    $SIG{TERM} = \&amp;amp;close_n_exit;&lt;BR /&gt;    while ($kontinue)&lt;BR /&gt;      {&lt;BR /&gt;        print `date`;&lt;BR /&gt;        sleep(10);&lt;BR /&gt;      }&lt;BR /&gt;  }&lt;BR /&gt;else&lt;BR /&gt;  { # parent process - just exit; the child is doing the real work&lt;BR /&gt;    exit($cc);&lt;BR /&gt;  }&lt;BR /&gt;exit($cc);&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Please help me to make the right script.&lt;BR /&gt;It will be points, of course :)</description>
      <pubDate>Tue, 28 Jul 2009 13:48:05 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/perl-daemon-script/m-p/4468288#M680123</guid>
      <dc:creator>Inesa Clinko</dc:creator>
      <dc:date>2009-07-28T13:48:05Z</dc:date>
    </item>
    <item>
      <title>Re: perl daemon script</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/perl-daemon-script/m-p/4468289#M680124</link>
      <description>&lt;!--!*#--&gt;Hi:&lt;BR /&gt;&lt;BR /&gt;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).&lt;BR /&gt;&lt;BR /&gt;Your code is decidedly C-like.  A much more Perl-ish version is this:&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/perl&lt;BR /&gt;use POSIX qw( setsid );&lt;BR /&gt;use strict;&lt;BR /&gt;use warnings;&lt;BR /&gt;my $logfile = '/var/tmp/mydaemon';&lt;BR /&gt;my $pid;&lt;BR /&gt;sub close_n_exit {&lt;BR /&gt;    print STDERR "going away...\n";&lt;BR /&gt;    exit 1;&lt;BR /&gt;} # close_n_exit&lt;BR /&gt;chdir '/' or die "Can't cd to /: $!\n";&lt;BR /&gt;open( STDIN , '&amp;lt;', '/dev/null' ) or die "Can't open /dev/null: $!\n";&lt;BR /&gt;open( STDOUT, '&amp;gt;', $logfile ) or die "Can't open '$logfile': $!\n";&lt;BR /&gt;$|++; #...set autoflush on STDOUT...&lt;BR /&gt;defined($pid = fork) or die "Can't fork(): $!\n";&lt;BR /&gt;exit if $pid;&lt;BR /&gt;setsid or die "Can't start a new session: $!";&lt;BR /&gt;$SIG{HUP} = 'IGNORE';&lt;BR /&gt;$SIG{QUIT} = 'IGNORE';&lt;BR /&gt;$SIG{INT} = 'IGNORE';&lt;BR /&gt;$SIG{PIPE} = 'IGNORE';&lt;BR /&gt;$SIG{TERM} = \&amp;amp;close_n_exit;&lt;BR /&gt;while (1) {&lt;BR /&gt;    print STDOUT scalar localtime, "\n";&lt;BR /&gt;    sleep 10;&lt;BR /&gt;}&lt;BR /&gt;exit 0&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Tue, 28 Jul 2009 14:42:26 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/perl-daemon-script/m-p/4468289#M680124</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2009-07-28T14:42:26Z</dc:date>
    </item>
    <item>
      <title>Re: perl daemon script</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/perl-daemon-script/m-p/4468290#M680125</link>
      <description>Thanks ,James</description>
      <pubDate>Wed, 29 Jul 2009 05:36:01 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/perl-daemon-script/m-p/4468290#M680125</guid>
      <dc:creator>Inesa Clinko</dc:creator>
      <dc:date>2009-07-29T05:36:01Z</dc:date>
    </item>
  </channel>
</rss>

