Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running External Command - Help with exit codes 1

Status
Not open for further replies.

kudithipudi

IS-IT--Management
Feb 11, 2002
29
0
0
A2
Hello Guys,
I am trying to run a command from perl using the backticks (``) as such

$COMMANDOUTPUT = `plink.exe -ssh -pw password root\@$FQDNSERVER \"service jakartaservice start\"`;

This command conntects to a remote server via SSH and then executes a command (service jakartaservice start) on the remote server. The problem I am having is that the jakartaservice does not return "control" to the console completely after starting. So my perl script is stuck near the $COMMANDOUTPUT. I was wondering if there was any way, I can kill the process after some time after launching it.

Any help is appreciated. Thanks much.

- V.
 
i do not know if youe ssh runs correctly,
to cach the exix-status i use:

$exit = system("the command you send") >>8;
exit(1) if($exit);
 
But the script hangs even when run locally. i.e. I am running the script on the server itself without the SSH part. Catching the exit code is a great idea, but if the command I run is not even releasing the script to go to the next step, I cannot get them. Thanks for the idea though.

- V.
 
Execute your statement in a child process, then kill the child after waiting some period of time.

For example:
[tt]if (my $pid = fork) {
# sleep 30 seconds
sleep 30;
kill 9, $pid;
} else {
system("plink.exe -ssh -pw password root\@$FQDNSERVER \"service jakartaservice start\"");
}
[/tt]
 
Thanks rosenk.. Exactly what I was looking for :)

- V.
 
Rosenk,
I am using your suggestion in my script to fork out a process and run the externel command from that process. The program runs fine, but it is not killing the child after 5 seconds as expected. I running this snippet under Windows 2000. Can you please take a look at my code and let me know, where I am wrong? Thanks much. I am going through a list of servers.

START
foreach $RSS (@SERVERS)
{
chomp($RSS);
Log("Processing $RSS","0","TOLOG");
Log("-------------------------","0","TOLOG");

# Resolve the FQDN of the server
$FQDN = ResolveIPToDNS($RSS) || Log ("Error resolving $RSS 's FQDN via DNS","0","TOLOG");
if ($FQDN eq "CANNOTRESOLVE")
{
Log("$RSS: BAD DNS","0","TOLOG");
$BadCount++;
next;
}

# Ping the server and check if it is up or not
$ReturnCode = PingNode($FQDN);
if ($ReturnCode eq "FAIL")
{
Log("$RSS: CANNOT PING [$FQDN]","0","TOLOG");
$BadCount++;
next;
}

# Check if ITRS is already running
# Construct URL to check
$URL = "
if (get $URL)
{
Log("$FQDN: ITRS Already Running","0","TOLOG");
$BadCount++;
}
else
{
#Do something with this sucker
if (my $pid = fork)
{
# sleep 30 seconds
sleep 5;
kill 9, $pid;
$GoodCount++;
}
else
{
system("plink.exe -ssh -pw $Password $User\@$FQDN \"service itrs start\"") || Log("Could not start ITRS!!","0","TOLOG");
}
}
Log("","0","TOLOG");
}

END
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top