Hello All,
I have inherited 14 linux and 2 HPUX production level machines in a testing environment. I need to be able to log into each of the 16 machines, launch a process to run in the background, and then return immediately while the program runs. The program takes 30-45 minutes to run. So, I'm try to ssh to the target machines and launch each run with hohup. In the code below, the first ssh->cmd runs in a few seconds. So, I don't care if I have to wait on that. The second ssh->cmd() is what runs a long time and I can't wait.
Also, so far, I'm just trying to get this to work hitting one target machine. I'll wrap a loop around it once I get the basics to work.
How do I get the ssh connection on the target/client end to execute the program in the background and close/release the ssh connection immediately?
I have done this before and made it work and it is driving me crazy that I can not remember the details.....
Anyway, thanks.
'hope this helps
If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
I have inherited 14 linux and 2 HPUX production level machines in a testing environment. I need to be able to log into each of the 16 machines, launch a process to run in the background, and then return immediately while the program runs. The program takes 30-45 minutes to run. So, I'm try to ssh to the target machines and launch each run with hohup. In the code below, the first ssh->cmd runs in a few seconds. So, I don't care if I have to wait on that. The second ssh->cmd() is what runs a long time and I can't wait.
Also, so far, I'm just trying to get this to work hitting one target machine. I'll wrap a loop around it once I get the basics to work.
How do I get the ssh connection on the target/client end to execute the program in the background and close/release the ssh connection immediately?
Code:
#!/usr/bin/perl
use strict;
use Net::SSH::Perl;
use Term::ReadKey;
$| = 1;
print "TARGET HOST: ";
my $host = <STDIN>;
print "USER: ";
my $user = <STDIN>;
print "PASSWORD: ";
ReadMode('noecho');
my $pass = ReadLine(0);
ReadMode('normal');
print "\nApplication Version to Run: ";
my $dev = <STDIN>;
chomp ($host, $user, $pass, $dev);
my $installer = '/redacted path info/'.$dev;
my $cmd = "$installer -a -b /opt/app/redacted/";
print "Install CMD: $cmd\n";
my $ssh = Net::SSH::Perl->new($host, {protocol=>2}) or die "Failed to create object, $!\n";
unless ($ssh->login($user, $pass)) { die "Failed to login, $!\n"; }
my ($stdout, $stderr, $exit) = $ssh->cmd($cmd);
$cmd = 'nohup /redacted path/executable_file_here -a 2>&1 &';
print "Run CMD: $cmd\n";
## I want the next line to launch the cmd in the
## background on the remote machine and return immediately.
my ($stdout, $stderr, $exit) = $ssh->cmd($cmd);
$stdout = ($stdout =~ /\w/) ? $stdout : 'Non-sensical response.' ;
$stderr = ($stderr =~ /\w/) ? $stderr : 'None.' ;
print "Response:\n$stdout\nErrors: $stderr\n";
I have done this before and made it work and it is driving me crazy that I can not remember the details.....
Anyway, thanks.
'hope this helps
If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.