CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
system command to run a application

 



hem
User

Mar 25, 2010, 8:39 PM

Post #1 of 14 (1147 views)
system command to run a application Can't Post

Hi

I am trying to run a application (.exe) from a perl script and i using the system command to to it.It opens the application but the problem is that the scripts just sits there waiting on something.I tried using exec but it does not continues on to the next line and exits the script.

Any ideas what "system" command is waiting on and why its just sitting there.
I am doing:
system($Command)
Thanks in advance


Nila
User


Mar 25, 2010, 9:09 PM

Post #2 of 14 (1144 views)
Re: [hem] system command to run a application [In reply to] Can't Post

Try to get the return value or error message from system command.
Read the perl document for exec and system functions. If system function fails only it will wait , otherwise it will not wait.

Try this,

Code
system($com)==0 or die "system $com failed: $?";



7stud
Enthusiast

Mar 25, 2010, 9:30 PM

Post #3 of 14 (1141 views)
Re: [hem] system command to run a application [In reply to] Can't Post


Quote
Any ideas what "system" command is waiting on and why its just sitting there.


Yes. system() waits for the other program to finish executing. When the other program finishes executing, system() returns the exit code of the other program. Typically, an exit code of 0 means success, and any other value means something went wrong.

Here is an example:

1perl.pl:

Code
use strict; 
use warnings;
use 5.010;

my $status = system '2perl.pl';
say $status;


2perl.pl:

Code
#!/usr/bin/env perl 

use strict;
use warnings;
use 5.010;

sleep 5;
exit 0;


output:

Code
$ perl 1perl.pl   
(hangs for 5 seconds)
0



(This post was edited by 7stud on Mar 25, 2010, 9:35 PM)


hem
User

Mar 25, 2010, 9:34 PM

Post #4 of 14 (1138 views)
Re: [7stud] system command to run a application [In reply to] Can't Post

so is it possible that the other program does not return anything . In that case will system just wait


7stud
Enthusiast

Mar 25, 2010, 9:37 PM

Post #5 of 14 (1135 views)
Re: [hem] system command to run a application [In reply to] Can't Post

Try running my example and commenting out the exit() call.


hem
User

Mar 26, 2010, 11:21 AM

Post #6 of 14 (1121 views)
Re: [7stud] system command to run a application [In reply to] Can't Post

i tried the script after commenting exit and it returned

But then i changed the system command to

use strict;
use warnings;


my $status = system 'notepad';
print " $status\n";

In this case the script hangs and waits until i kill notepad or ctrl C

Thanks


7stud
Enthusiast

Mar 26, 2010, 11:49 AM

Post #7 of 14 (1120 views)
Re: [hem] system command to run a application [In reply to] Can't Post

Notepad doesn't finish running until you close Notepad. When Notepad first appears, that doesn't mean it is done running. You can still type text into a Notepad file, save it, edit it, etc., right? That's because Notepad is still running. Notepad is what's called a "gui program". Gui programs run in an infinite loop, so they won't stop running unless you take some action to kill them.

Both system() and exec() wait for the specified program to finish executing. It sounds like what you are looking for is fork(). See here:

http://www.onperl.net/Starting-A-Child-Process


(This post was edited by 7stud on Mar 26, 2010, 12:02 PM)


FishMonger
Veteran / Moderator

Mar 26, 2010, 12:00 PM

Post #8 of 14 (1111 views)
Re: [hem] system command to run a application [In reply to] Can't Post

It's unclear as to what your ultimate goal is, but maybe this is what you're needing.

Win32::Process - Create and manipulate processes.
http://search.cpan.org/~jdb/Win32-Process-0.14/Process.pm


hem
User

Mar 26, 2010, 2:03 PM

Post #9 of 14 (1102 views)
Re: [FishMonger] system command to run a application [In reply to] Can't Post

ok i changed the code to
use warnings;
use strict;
use Win32::Process;
use Win32;

sub ErrorReport{
print Win32::FormatMessage( Win32::GetLastError() );
}
my $ProcessObj;
Win32::Process::Create($ProcessObj,
"C:\\NetMeter\\Myapp.exe",
"notepad temp.txt",
0,
NORMAL_PRIORITY_CLASS,
".")|| die ErrorReport();


sleep(5);

my $exitcode;
Win32::Process::KillProcess($pid, $exitcode);

Where do i get $pid from ?


Thanks

Code



      
    


7stud
Enthusiast

Mar 26, 2010, 3:54 PM

Post #10 of 14 (1094 views)
Re: [hem] system command to run a application [In reply to] Can't Post

I imagine it's returned by Create().


hem
User

Mar 26, 2010, 4:07 PM

Post #11 of 14 (1092 views)
Re: [7stud] system command to run a application [In reply to] Can't Post

Looked up Win32::Process::Create($obj,$appname,$cmdline,$iflags,$cflags,$curdir)


return is
Returns non-zero on success, 0 on failure.


FishMonger
Veteran / Moderator

Mar 26, 2010, 6:13 PM

Post #12 of 14 (1090 views)
Re: [hem] system command to run a application [In reply to] Can't Post


Code
#!/usr/bin/perl 

use warnings;
use strict;
use Win32::Process;
use Win32::Process::Info;

my $pi = Win32::Process::Info->new;

Win32::Process::Create(my $ProcessObj,
'C:\winnt\system32\notepad.exe',
"notepad temp.txt",
0,
NORMAL_PRIORITY_CLASS,
".")
or die ErrorReport();

my @info = grep {$_->{Name} =~ m/notepad/}
$pi->GetProcInfo();
my $pid = $info[-1]{'Handle'};

print $pid;

sleep 5;
my $exitcode = 0;
Win32::Process::KillProcess($pid, $exitcode);

sub ErrorReport{
print Win32::FormatMessage( Win32::GetLastError() );
}



rovf
Veteran

Mar 27, 2010, 9:04 AM

Post #13 of 14 (1081 views)
Re: [hem] system command to run a application [In reply to] Can't Post

Don't really the answer for your question, but maybe helpful too:

I don't use Win32::Process for this, but a special feature of the system command (passing the magical integer '1' as first parameter), for instance:

$pid = system(1, $cmd);

This starts $cmd as separate process. To check whether the process has been ended, use

use POSIX qw( WNOHANG );
$program_finished=waitpid($pid, WNOHANG);

To kill the process, use

kill(-9,$pid)

or, if you are gentle, send first signal 15 to give the process the chance to save the file, and fire with -9 if the process decided to ignore you.


FishMonger
Veteran / Moderator

Mar 27, 2010, 10:27 AM

Post #14 of 14 (1078 views)
Re: [rovf] system command to run a application [In reply to] Can't Post


In Reply To
Don't really the answer for your question, but maybe helpful too:

I don't use Win32::Process for this, but a special feature of the system command (passing the magical integer '1' as first parameter), for instance:

$pid = system(1, $cmd);


Nice...I completely forgot about that.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives