Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Bad file descriptor after vmran command using system()

by Ingvar (Novice)
on Apr 08, 2013 at 12:43 UTC ( #1027489=perlquestion: print w/replies, xml ) Need Help??
Ingvar has asked for the wisdom of the Perl Monks concerning the following question:

Dear Experts, I have been testing a simple portion of a script that is supposed to become part of something else and suddenly run into a problem: after I delete the Vmware Workstation snapshot using system() I get Bad file descriptor message. But the snaphot itself is successfully deleted! And the same command from the command prompt works all right. Here is my code:

#!/usr/bin/perl use Time::HiRes; use File::Path; use warnings; use strict; $|=1; sub spin { my ($arg) = @_; for (my $x = 0; $x <= $arg; ++$x) { foreach (("-", "\\", "|", "/")) { print $_; Time::HiRes::sleep (0.1); print "\b"; } } #print " \n"; } my $j = 0; my $base = "C:/Users/User/Documents/Virtua~1/"; print "Enter VM name: "; my $vm = <STDIN>; chomp ($vm); print "Enter Snapshot to be deleted: "; my $snapshot = <STDIN>; chomp ($snapshot); my $vmware_dir = "C:/Progra~2/Vmware/VMware~1/"; my $command = "$vmware_dir" . "vmrun -T ws deleteSnapshot " . "$base" +. "$vm" . "/" . "$vm" . ".vmx " . "$snapshot"; if ( (my $pid = fork() ) == 0) { do { sleep 0.5; spin($j) } while 1; } else { print "Deleting snapshot..."; system ("$command") or die "Operation failed: $!\n"; kill 9, $pid; waitpid $pid, 0; } print "\bDone!\n"; # Never gets here!

And this is what I get in the end:

Deleting snapshot...\Operation failed: Bad file descriptor

As I said, the snapshot gets deleted, but there is an error message and the background spinning never ends.
Thank you in advance!

Replies are listed 'Best First'.
Re: Bad file descriptor after vmran command using system()
by MidLifeXis (Monsignor) on Apr 08, 2013 at 12:54 UTC

    Your expectations for the return value of system are not correct. It returns 0 in the case of success, and therefore your test should look like:

    system( ... ) == 0 or die "System failed: $?, $!"


      It works now! Thank you all!

Re: Bad file descriptor after vmran command using system()
by daxim (Chaplain) on Apr 08, 2013 at 12:52 UTC
    You're using system() incorrectly, specifically its return value. The error message is misleading, $! contains junk from a previous system call. Using system() correctly is very difficult, have a peek at the official documentation. All Perl built-ins suck, this one sucks the most.

    Instead use autodie for proper error handling, it's tested and you get nice default error messages:

    use autodie qw(:all); # replaces system() system $command; # implicit error handler

      All Perl built-ins suck, this one sucks the most.

      Low level functions are low level functions, this doesn't mean they suck, they're just low level

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1027489]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2018-05-23 09:35 GMT
Find Nodes?
    Voting Booth?