Why can't I spawn a simple command with node.js? - command-line

pipe(): Too many open files
node.js:134
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Error spawning
That's my error. My code is:
grep = spawn 'ls'
UPDATE
for tweet in indexedTweetsResults
exec 'ls', (error, stdout, stderr) ->
console.log stdout
This is my code and it errors with the pipe error. Any ideas?

YOu need to do
exec = require("child_process").exec
and then somewhere do
exec("ls", function(error, stdout, stderr) {
// handle error, stdout, stderr
});
you have to write javascript....

Related

Querying Perl Output

I need to query on the 2nd line which I am seeing in the output. I need to check if the command returns on "Listener LISTENER is running on" and provide the desired output. My code is able to read the first line and not the second line which I need to verify. Please advise.
Added a While statement so it can read the 2nd line, did not work.
use strict;
use warnings;
my $cmd="srvctl status listener";
my $listenerstatus0;
my $msg0;
my $msg1;
open(Row1Stat,"$cmd |") || die ("Could not read the pipe\n");
$listenerstatus0 = <Row1Stat>;
close(Row1Stat);
if( $listenerstatus0 =~ m/Listener LISTENER is running/)
{
$msg0="LISTENER is running";
$msg1=1
}
elsif ($listenerstatus0 =~ m/Listener LISTENER is not running/) {
$msg0 = "LISTENER is not running";
$msg1 = 0;
}
else {
$msg0 = "Unable to Query LISTENER Status";
$msg1 = 0;
}
print "\nStatistic.Name1:$msg1";
print "\nMessage.Name1:$msg0";
Below is the Output of the command, I need to check the 2nd line.
srvctl status listener
Listener LISTENER is enabled
Listener LISTENER is running on node(s): XYZ
The script needs to check "Listener LISTENER is running " and exit out with the exit code as defined in the script.
If you want to read two lines you shouldn't stop reading after the first:
open(Row1Stat,"$cmd |") || die ("Could not read the pipe\n");
$ignorethis= <Row1Stat>; # Read 1st line
$listenerstatus0 = <Row1Stat>; # Read 2nd line
close(Row1Stat);

SOAP::LIte on_fault not overriding default error handling

Centos 5 |
Perl 5.10.0 |
SOAP::Lite 1.20
Having read the documentation for using on_fault as an override to the default error handling of SOAP::Lite, I would expect the following code to use the callback for error handling processing. However, what I see happening is the default is being used
#!/usr/bin/perl
use strict;
use SOAP::Lite;
my $log #calls to Log4Perl
my $soapServer = "http://somelocation/services/GdeWsOpenAPI?wsdl"
my $soap = new SOAP::Lite();
$soap->on_fault( \&soapError );
$soap->service($soapServer);
sub soapError {
my($soap, $res) = #_;
my $message = ref $res ? $res->faultstring : $soap->transport->status;
$log->write( "fatal connection error to server $SoapServer: $message.", 0);
print STDERR "connection error: $message\n";
exit 1;
}
Output is:
Service description 'http://somelocation/services/GdeWsOpenAPI?wsdl' cannot be loaded: 500 Can't connect to somelocation:80
Expected (because of transport error):
connection error: Service description 'http://somelocation/services/GdeWsOpenAPI?wsdl' cannot be loaded: 500 Can't connect to somelocation:80
What am I missing?
The callback is for problems that occur when making a SOAP call. You haven't gotten that far.
my $soap = SOAP::Lite->new();
$soap->on_fault( \&soapError );
eval { $soap->service($soapServer); 1 }
or die("Can't initialize the web service: $#");
A fault is a special type of response from the server saying "something went wrong with your request". What's happening there is not a fault, it's failing to connect to the server at all. You might want to use Try::Tiny for this.

Expect script output at Perl console

I have a exp script -script.exp to enter a name and password. it calls test.sh file like below.
set timeout -1
spawn ./test.sh -create
match_max 100000
expect -exact "Enter the name: "
send -- "abcd\r"
expect -exact "\r
Please confirm password: "
send -- "xxy\r"
expect -exact "\r
expect eof
It gives me a output at expect window -
"Successfully entered the name"
if some issue there it throws exception or error message.
I run this expect script in a Perl file-- like below
$cmd="expect script.exp";
system($cmd);
$outputfromexp=?
I need a output of fail or passed status of exp at Perl console after running the script.
How can i do this? Please help me.
I tried calling as suggested in my Perl script--
use strict;
use warnings;
sub sysrun {
my ($command) ="expect script.exp";
my $ret_code;
$ret_code = system("$command");
if ( $ret_code == 0 ) {
# Job suceeded
$ret_code = 1;
}
else {
# Job Failed
$ret_code = 0;
}
return ($ret_code);
}
my $ret_code=sysrun();
print "reuturmsfg- $ret_code\n";
But its printing nothing-- just reuturmsfg-.
I made the changes-
my $ret_code=sysrun();
it gives me 0 and 1 return code.
If I understand the question correctly, to get the return code you want to do:
system($cmd);
my $ret_code = $?;
To get STDOUT output from the execution:
my #out = `$cmd`;
my $ret_code = $?;
I strongly suggest using strict and warnings in your code.

if condition using telnet in perl not working

I'm trying to use if condition to check if command has passed, but its not working. Even though the mount has been successful it goes to failed message. When i enter this command, it retruns to the prompt without any message, hence i'm comparing with a "". And when i do a "ls" of destination folder, it shows all contents of source folder. Any help? Is my if condition correct?
my $port = new Net::Telnet->new(Host=>$ip,Port=>$ip_port,Timeout => "$timeout", Dump_Log => "dumplog.log", Errmode=> "return" );
if($port->cmd("mount -t nfs -o nolock <path-of-source-folder> <destination-folder>") eq "")
{
print "Successful\n";
}
else{
print "Failed.\n ";
}
In scalar context, the Net::Telnet cmd method returns 1 on success (not a string). Your check should be something like:
if ($port->cmd("mount -t nfs -o nolock <path-of-source-folder> <destination-folder>") == 1)
{
print "Successful\n";
} else {
print "Failed.\n";
}
If you actually want to collect the output from the mount command and inspect it, you will have to either call it in list context or pass a stringref argument, like so:
my #outlines = $port->cmd("mount ...");
Or:
my $out;
my $ret = $port->cmd("mount ...", [Output => \$out]);
if ($ret == 1)
{
# inspect $out
}
See the Net::Telnet documentation for more.
Your check for the result seems to be wrong. The documenation of Net::Telnet says that
This method sends the command $string, and reads the characters sent back by the command up until and including the matching prompt. It's assumed that the program to which you're sending is some kind of command prompting interpreter such as a shell.
The command $string is automatically appended with the output_record_separator, by default it is "\n". This is similar to someone typing a command and hitting the return key. Set the output_record_separator to change this behavior.
In a scalar context, the characters read from the remote side are discarded and 1 is returned on success.
So you need to check in a scalar context
if ($port->cmd("..") ) {
...
}

Retrieve exception message from postgresql function

I have a trigger function on a table that runs on inserts which for certain circumstances will raise an exception.
I maintain an old Perl application running on Catalyst that creates a transaction and inserts rows on the table.
When the trigger function raises an exception, I'd like to be able to print out just the error message I throw and not any debugging information (database operation, context, perl file, etc).
So for example, if my function throws something like:
raise exception 'Item with id % cannot be shipped at this time.', new.id;
I would like to only see
Item with id 13 cannot be shipped at this time.
and not
DBIx::Class::Row::insert(): DBI Exception: DBD::Pg::st execute failed: ERROR: Item with id 13 cannot be shipped at this time. [for Statement "INSERT INTO ... at /home/../lib/Class/Controller/Inv.pm line 260
The perl code is currently something like
$c->model('Class')->schema->txn_do(sub {
...
eval {
$shipment->insert;
1;
} or do {
$error = $#;
last;
};
if ($error) {
$c->stash->{error} = $error;
}
);
Thank you.
Perhaps this substitution:
my $error = $#;
$error =~ s/^.*ERROR: (.*) \[for Statement.*$/$1/;
You could access the errstr() method of the database handle, which is what is what is passed to warn/die anyway
warn $c->model('Class')->schema->storage->dbh->errstr();