in reply to Re^3: Running Perl scripts from ssh
in thread Running Perl scripts from ssh

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re^5: Running Perl scripts from ssh
by Argel (Prior) on May 08, 2006 at 23:17 UTC
    Here's some code that should work for you. Add this subroutine to your code and then replace your call to fork with a call to daemonize instead.
    use IO::Socket; use POSIX qw(WNOHANG setsid); sub daemonize { $SIG{CHLD} = 'IGNORE'; # Configure to autoreap zombies die "Can't fork" unless defined ( my $child = fork ); # FORK +<<<<<<<<<<<< CORE::exit(0) if $child; # Parent exits setsid(); # Become session leader open( STDIN, "</dev/null" ); # Detach STDIN from shell open( STDOUT, ">/dev/null" ); # Detach STDOUT from shell open( STDERR, ">&STDOUT" ); # Detach STDERR from shell chdir '/tmp'; # Change working directory umask(0); # Reset umask $ENV{PATH} = '/bin:/sbin:/usr/sbin'; # Reset PATH }
    WARNING: Verify your system supports autoreaping of zombies!!!!!

    You can read more in these threads: 201937, 539098.

      I am unable to use this function in proper way.. When I am calling this function instead of fork , it just detaches from ssh session but cant process the exact function system(...) that we are really intended for.. I am giving u the modified code... Please come up with exact implementation of daemonize() which will both run the intended function in background..and detatch from ssh session.
      #!/usr/bin/perl -w use strict; use warnings; use Getopt::Long; #variable declaration my $exec_mode; my $param; my $paramfile; my $log_loc= "/logs/default_log.txt"; my $interv =2; GetOptions( "e=s" => \$exec_mode, "p=s" => \$param, "pf=s" => \$paramf +ile, "l=s" => \log_loc, "i=i" => \$interv); if($exec_mode eq 'start') { if( $paramfile eq NULL) { &startsar ($param, $interv); } else { &startsar ($paramfile, $interv); } elsif($exec_mode eq 'stop') { &stopsar ( $log_loc); } else { print " usage sarexec -arguemrnts are-\n -e <start>/<stop>\n -p <p +arams> \n -pf <param_file> \n -l <location> (by default location is ' +/logs/default_log.txt')\n -i <interval> ( by default interval is 2)\n + " } sub startsar { if (substr($_[0],0,1) eq "-") { my $pid = fork(); if($pid>0) { #parent } else { system("sar -o logfile $_[0] $interval 0 + > /dev/null&"); # this utility must run in background and able to cl +ose the script exit 0; } } else { chomp $_[0]; my $param; open FH,"< $_[0]" or die"cant open the fil +e"; $param = <FH>; close FH or die "cant close the file"; chomp($param); my $pid = fork(); if($pid>0) { #parent } else { system("sar -o logfile $param $interval + 0 > /dev/null&"); # this utility must run in background and able to +close the script exit 0; } } } sub stopsar { system("kill \$(pidof sadc)"); system("sar -f logfile>>$_[0]"); }
        You need to call daemonize before you start running other stuff. You should split your GetOptions subroutine into two. One that just gets the options and does any inilization stuff (e.g. variable assignment) that's needed. Then call daemonize. And then start doing your system calls.