agaream has asked for the wisdom of the Perl Monks concerning the following question:

I am putting this question once again because I was unable to solve from problem by the replies I got earlier. Here I am providing you with modified code of my script I want to run this script from remote machine through ssh. (ssh user@machine_name perl sar1.pl -e start -p -wW -i 3) this way... But I am unable to run this script, while if It is running fine on local machine ( also by logging into remote machine through ssh shell and running from the shell) But I want this to run directly as I specified earlier.. I know from earlier replies that some part of the script is running and it is unable to detach from ssh . Kindly reply with solutions.. Here is entire code of my script-
#!/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]"); }

Edited by planetscape - added link to previous question, to which this is a followup

( keep:1 edit:16 reap:0 )

Replies are listed 'Best First'.
Re: Running Perl Script on ssh from remote machine
by UnderMine (Friar) on May 12, 2006 at 11:29 UTC
    Not sure but this looks like a disowning issue in that so long as the child is associated witht the parent the ssh channel is still open. A quick fix maybe to wrap the call to sar in a small shell script that starts it up and then disowns the child. UnderMine
Re: Running Perl Script on ssh from remote machine
by graff (Chancellor) on May 13, 2006 at 05:19 UTC
    I'm confused by the command line that you say you want to use:
    ssh user@machine_name perl sar1.pl -e start -p -wW -i 3
    Is it the case that you are really using a parameter file whose name is "-wW"? Why?

    Anyway, if the script runs as intended with that command line when you run it in an ssh login shell on the remote machine, then maybe your environment via "ssh you@host command line" is different from the login shell environment that you get when you just do "ssh you@host".

    You could test that by doing the following from your local host:

    local$ ssh you@remote.host remote$ env > my-remote-login.env remote$ exit local$ scp you@remote.host:my-remote-login.env . local$ ssh you@remote.host env > my-remote-nonlogin.env local$ diff my-remote-login.env my-remote-nonlogin.env
    Sometimes, the login connection uses a different or extra shell script (e.g. .bashrc) compared to what a non-interactive connection uses (e.g. .bash_profile); and sometimes this could affect how PATH is set, etc.
Re: Running Perl Script on ssh from remote machine
by jesuashok (Curate) on May 12, 2006 at 11:30 UTC
    hi

    > I want to run this script from remote machine through ssh. (ssh user@machine_name perl sar1.pl -e start -p -wW -i 3) this way..
    which is not possible for any executables.

    Instead of using system functions, try to use SSH related modules in your script and make the code work fine as per your requirement.

    "Keep pouring your ideas"