Execute Windows program from Perl script (??)

T

Tony McGuire

'Server' is win2k.

I've looked at exec() as well as system().

When I exec("dir"); I get an error log entry that includes the start
of the dir command response. So I *might* be able to figure it out
from there, but so far no luck. I've read several posts as well as
articles about exec() as well as system(), but haven't had any luck.

What I want is to launch a (Windows) program from a perl script and
for the perl script to continue on. No response from the external
program.

Also, the actual program I wish to execute is in c:\program
files\etc\etc.exe

When I try to exec() or system() it, it chokes at the first space
(after 'program') and reports "'C:program' is not recognized as an
internal or external command." in the error log.

Does anyone have info on how to (whether I can) get perl to execute an
external program and continue on?

Any help appreciated.
 
B

Brian McCauley

Tony said:
'Server' is win2k.

I've looked at exec() as well as system().

When I exec("dir"); I get an error log entry that includes the start
of the dir command response. So I *might* be able to figure it out
from there, but so far no luck. I've read several posts as well as
articles about exec() as well as system(), but haven't had any luck.

What I want is to launch a (Windows) program from a perl script and
for the perl script to continue on. No response from the external
program.

Also, the actual program I wish to execute is in c:\program
files\etc\etc.exe

When I try to exec() or system() it, it chokes at the first space
(after 'program') and reports "'C:program' is not recognized as an
internal or external command." in the error log.

Does anyone have info on how to (whether I can) get perl to execute an
external program and continue on?

This is not a question about Perl, it is a question about the host
operating system's (in this case Window's) command line interpreter to
which system() give you access.

You enclose it in double quotes.
 
T

Tony McGuire

Brian McCauley said:
This is not a question about Perl, it is a question about the host
operating system's (in this case Window's) command line interpreter to
which system() give you access.

It isn't? "How do you do XXX in Perl?" isn't appropriate to this
forum? Do the questions all have to be a technical discussion of a
function, rather than how to get a desired result from a function?

You enclose it in double quotes.

And that doesn't work. I tried that, and several variations. The
best I get is a hung browser and a copy of cmd.exe in task mangler for
each attempt I make.

So, now that I've been chastised for asking the wrong question in the
wrong place, and given an answer that doesn't work, is there anyone
willing to inform me of just which group I CAN ask this on - and not
get put down for not knowing ahead of time what I came here in hopes
of learning?
 
S

Sisyphus

Tony said:
It isn't? "How do you do XXX in Perl?" isn't appropriate to this
forum? Do the questions all have to be a technical discussion of a
function, rather than how to get a desired result from a function?





And that doesn't work. I tried that, and several variations. The
best I get is a hung browser and a copy of cmd.exe in task mangler for
each attempt I make.

So, now that I've been chastised for asking the wrong question in the
wrong place, and given an answer that doesn't work, is there anyone
willing to inform me of just which group I CAN ask this on - and not
get put down for not knowing ahead of time what I came here in hopes
of learning?

I don't think Brian was chastising you ... he was merely informing you :)

I assume that he is correct - though I'm not prepared to argue one way
or the other.

Anyway, I put a simple little executable called 'a.exe' in C:\Program
Files\Winamp and ran the following perl script:

use warnings;
system("C:\\Program Files\\Winamp\\a.exe");
__END__

That worked fine. Maybe we need to see the actual system command that
you're running - and also the actual error you get (when using double
quotes).

Btw, putting perl in a folder whose name includes white space is a bad
idea. If you have any say in where perl gets put, don't let it be in
such a folder.

Cheers,
Rob
 
T

Tony McGuire

Sisyphus,

: That worked fine. Maybe we need to see the actual system command that
: you're running - and also the actual error you get (when using double
: quotes).

system( "start", "C:\Program Files\TextPad 4\TextPad.exe");

There is no error. The web page I post from just sits there as if the perl
were executing. And when I check the 'server' box I find a cmd.exe in Task
Mangler, one for each time I run the script.

And the only way to remove the 'hidden' copy of cmd.exe (there's no DOS box
up) is to reboot Windows. When I try to end task on them, I get 'Access
Denied'.
 
J

Jürgen Exner

Tony said:
It isn't? "How do you do XXX in Perl?" isn't appropriate to this
forum?

To call an external program from Perl you use system() or qx or backticks or
exec or ...
However, that is not your problem.
Do the questions all have to be a technical discussion of a
function, rather than how to get a desired result from a function?

Sure. But your problem is with the OS resp. with how the command interpreter
of your OS interprets a command line. It is not a Perl problem.
And that doesn't work. I tried that, and several variations.

Could you please show us?
The
best I get is a hung browser and a copy of cmd.exe in task mangler for
each attempt I make.

Browser? Browser? Where do you have a browser?
So, now that I've been chastised for asking the wrong question in the
wrong place, and given an answer that doesn't work,

Actually, I presume that you interpreted the answer wrong.
Earlier you wrote:
Also, the actual program I wish to execute is in
c:\program files\etc\etc.exe
indicating that the command "c:\progam" wasn't found or something along that
line.

Well, if you call it like
system("c:\program files\etc\etc.exe")
then you got a double quoted Perl string but the command itself is by no
means enclosed in double quotes.
Just check on your command line what happens when you enter
c:\program files\etc\etc.exe
In other words: the Windows command interpreter (not Perl !!!) requires you
to enclose the _command_ in double quotes like
"c:\program files\etc\etc.exe"

And that string *including*those*double*quotes* is the text that you have to
pass to system() as argument
system("\"c:\program files\etc\etc.exe\"")
or
system('"c:\program files\etc\etc.exe"')
is there anyone
willing to inform me of just which group I CAN ask this on - and not
get put down for not knowing ahead of time what I came here in hopes
of learning?

Brian did answer your question. You just didn't read his reply carefully
enough.

jue
 
B

Brian McCauley

Jürgen Exner said:
system("\"c:\program files\etc\etc.exe\"")

Do not rely on \p and \e not being defined escapes.

system("\"c:\\program files\\etc\\etc.exe\"")

(I assume you, Jürgen, knew that really but it's worth correcting for
the sake of anyone else reading).
 
B

Brian McCauley

Tony said:
Sisyphus,

: That worked fine. Maybe we need to see the actual system command that
: you're running - and also the actual error you get (when using double
: quotes).

system( "start", "C:\Program Files\TextPad 4\TextPad.exe");

There is no error.

Well not a fatal error, but there is a warning. So either you forgot to
enable warings or you frogot to look in the log to see if there were any
errors.

Right the first thing to note is that there are several different forms
of system. From the error you gave in the first post it was evident
that you were using the single argument form. The multiple argument
form is great because it should bypass the host OS CLI but it would seem
on Windows it doesn't. For this reason it may be safer to stick with
the single argument form (or use one of the Win32::* modules to get
direct access to the lower level Windows API).

When using the single argument from of system() the value of string you
pass to system() must be exactly what you'd need to type at the command
prompt. Note: it is the _value_ of the string I'm talking about here -
not the representation of that string in Perl source code. If this
distinction is lost on you consider:

my $s = "foo\bar";

The representation of this string in Perl source code consists of 9
characters (the first and last being double quotes). The double qoutes
are not however _in_ the string. The value of the string is the the 6
characters 'f' 'o' 'o' said:
The web page I post from just sits there as if the perl
were executing.

Sorry, I hadn't clocked your original question as a stealth CGI
question. (Please don't do hat, see the posting guidelines).

From the name TextPad.exe looks like it may be a GUI interactive
program. Since web servers are usually run as background daemons
("services" in Windows-speak) then any processes started by them will
not be connected to a display ("worksation" in Windows-speak). How to
get a background job to interact with a display is very much OS
specific. In windows I've seen how it's done and it's a seriously
complicated process because you are going completely against the Windows
secrity model. The trick I usually use is to kick off a service that
runs a local system (interact with desktop) which then logs in as a user
and then kicks off the job. This is sloppy and invoves passing login
credentials around in plaintext. As I say I've seen more elegant
solutions but were drifting way OT here - this is all about the Windows
API - nothing to do with the programming language you happento be using.
And the only way to remove the 'hidden' copy of cmd.exe (there's no DOS box
up) is to reboot Windows. When I try to end task on them, I get 'Access
Denied'.

Yes, this is a well known bug in Windows. Actually you can get a
command line program called 'pskill' that doesn't have this problem.
(This, of course, has nothing to do with Perl).
 
J

Jürgen Exner

Brian said:
Do not rely on \p and \e not being defined escapes.

system("\"c:\\program files\\etc\\etc.exe\"")

(I assume you, Jürgen, knew that really but it's worth correcting for
the sake of anyone else reading).

Ouch, sorry. Didn't even see that.
Thank you!

jue
 
T

Tony McGuire

Brian McCauley said:
Sorry, I hadn't clocked your original question as a stealth CGI
question. (Please don't do hat, see the posting guidelines).

I wish I knew what you meant, here.

I'm trying to start a program (Textpad.exe is a text editor) from a
perl script. The perl script is executed with a web page. I am
trying to load that program on the web server. The web server is a
Windows box.

Am I doing it wrong by trying to use perl? Should I for some reason
be using CGI?

I haven't a clue why or where you use one instead of the other. I
just found the examples for uploading files, and it was all perl; so
that's what I stuck with.
 
J

Jürgen Exner

Tony said:
I wish I knew what you meant, here.

I'm trying to start a program (Textpad.exe is a text editor) from a
perl script.

So far so good, that's easy (using Notepad as a substitute because I
couldn't test it with Textpad.exe):

use warnings; use strict;
system ('notepad.exe');
The perl script is executed with a web page.

This part is, well, at least ambiguous. Web pages are HTML, they don't
"execute" other programs.
Maybe you are talking about DHTML, maybe you are talking about ActiveX
controls, maybe you are talking about ASP, maybe you are talking about CGI
scripts?
I am
trying to load that program on the web server. The web server is a
Windows box.

Now, what do you mean by "load that program"? Do you want to execute that
program on the server? Then I guess you are talking about a CGI script that
is written in Perl?
Would be a bit unusual to run an interactive text editor on a server that is
tucked away in a basement without even a terminal attached to it, but of
course it's your choice.

From a Perl point of view that shouldn't be much different from above. Of
couse now you got all the added complexity of web servers, CGI, etc. You
better ask in a NG that is dedicated to those topics, because people there
will be much more knowledgable about it then people in a Perl NG.

Just remember that system() doesn't return until the called program is
terminated. This might be difficult to do on a server. Do you really want to
go down there and terminate the program every time someone visits that web
page?
If you want to continue running the Perl script concurrently to the external
program then check out fork() and exec().
Am I doing it wrong by trying to use perl? Should I for some reason
be using CGI?

This is like asking "Am i doing it wrong by using a car? Should I be using
green instead?"
I haven't a clue why or where you use one instead of the other.

There is no "instead of", those terms belong into different universes.
Perl is a programming language. CGI is the Common Gateway Interface. They
have nothing to do with each other except that some CGI programs (quite a
few actually) happen to be written in Perl.
Just like cars and the color green. Although some cars happen to be painted
green, the one really has nothing to do with the other and the question
"should I use green instead of a car" doesn't make much sense.

jue
 
I

Ian Wilson

system('"c:/Program Files/Internet Explorer/IEXPLORE.EXE"')

works for me, and I find "/" looks clearer than "\\". YMMV
 
T

Tony McGuire

(e-mail address removed) (Tony McGuire)
system( "start", "C:\Program Files\TextPad 4\TextPad.exe");

There is no error. The web page I post from just sits there as if the perl
were executing. And when I check the 'server' box I find a cmd.exe in Task
Mangler, one for each time I run the script.

And the only way to remove the 'hidden' copy of cmd.exe (there's no DOS box
up) is to reboot Windows. When I try to end task on them, I get 'Access
Denied'.

I guess what I am looking for isn't doable from perl.

I was looking to start a Windows program independent of the Apache
umbrella. This program would then rewrite the config file for Apache
and restart Apache.

But I doubt Apache will restart if there is a process running, which
there would be if perl must wait on anything it starts with system().

Well, back to the drawing board. I guess I'll write to a file (with
perl), and have a scheduler watch for the file to be changed - then
kick off that other Windows program using the scheduler. That way the
Windows program is running independently of Apache and the perl
script.

I was hoping to coordinate everything with just perl, but it doesn't
seem to be up to this particular task.
 
J

Jürgen Exner

Tony said:
there would be if perl must wait on anything it starts with system().

That is putting the cart before the horse!
If you would have bothered to read the documentation for the functions you
are using you would have noticed:

system LIST
system PROGRAM LIST
Does exactly the same thing as "exec LIST", except that a fork
is done first, and the parent process waits for the child
process to complete. [...]

In other words: waiting for the called program to terminate is not a
limitation but a designed and wanted feature of system(). How else would you
return the return value of the called program anyway?
If you want a different behaviour then use a different function.

Complaining that the multiplication operator doesn't substract is kind of
beside the point.

jue
 
B

Brian McCauley

Tony said:
(e-mail address removed) (Tony McGuire)




I guess what I am looking for isn't doable from perl.

What in this thread makes you guess that?
I was looking to start a Windows program independent of the Apache
umbrella.

Well that is a question about Windows - having worked what succession of
windows API calls was necessary to achive that effect you could then
make those calls from a program written in any lanuage that gives you
access to the low level Windows API. This would include Perl with the
relevant Win32::* modules intalled.
But I doubt Apache will restart if there is a process running, which
there would be if perl must wait on anything it starts with system().

There are other windows-specific ways to start processes from a Perl
script as I mentioned earler in this thread.
Well, back to the drawing board. I guess I'll write to a file (with
perl), and have a scheduler watch for the file to be changed - then
kick off that other Windows program using the scheduler.

And there is, of course, no reason why that program can't be written in
Perl. Just like there's no reason why your CGIs can be written in C.
That way the Windows program is running independently of
Apache and the perl script.

I think you mean the CGI script. Yes it may be the case that your CGI
script is in Perl but the issues you are facing here would be the same
is you CGI script was written in C, Python, Java, FORTRAN etc.
I was hoping to coordinate everything with just perl,

There is no reason why you should not.
but it doesn't seem to be up to this particular task.

What causes you to imagine that?
 
T

Tony McGuire

Tony said:
there would be if perl must wait on anything it starts with
system().
That is putting the cart before the horse!
If you would have bothered to read the documentation for the functions you
are using you would have noticed:
system LIST
system PROGRAM LIST
Does exactly the same thing as "exec LIST", except that a fork
is done first, and the parent process waits for the child
process to complete. [...]

In other words: waiting for the called program to terminate is not a
limitation but a designed and wanted feature of system(). How else would you
return the return value of the called program anyway?
If you want a different behaviour then use a different function.

Complaining that the multiplication operator doesn't substract is kind of
beside the point.
Jue

What you guys don't seem to understand is that there some of out here
that don't have the *slightest* idea what the terms for a function
even are.

So how can we even search for them?

And therefore, how do we know we are using, or asking how to use, the
wrong function?

I've tried to explain what I am trying to do.

What I keep getting is correction of capitalization, correction of use
of terms, explanation of why what I am saying makes no sense when
approached from a technical standpoint, and finally a "don't do that
cause it won't work" with not even a pointer to what *might* work.

You expect us to understand ALL terminology. As far as I am concerned
a fork is something you eat with. I read several descriptions of the
perl meaning, but all they do is point to more gobledygook.

Too bad everyone in the perl world is taking their cues from the Linux
world.

An actual example of this stuff would go a loooong way to making it so
us idiots didn't need to bother the NG clic so much.
 
T

Tony McGuire

Jürgen Exner said:
This part is, well, at least ambiguous. Web pages are HTML, they don't
"execute" other programs.
Maybe you are talking about DHTML, maybe you are talking about ActiveX
controls, maybe you are talking about ASP, maybe you are talking about CGI
scripts?

No, I am talking about a simple web page <form> that directly posts to
the .pl file.

<FORM action='/cgi-bin/execprog.pl' method='POST'>

So, maybe what I actually have is a cgi script with a .pl extension,
with perl statements in it.

I have no idea.

But see, that's why I came to this group. I was hoping to find some
kind sole who could help me to understand why what I was trying wasn't
working, and maybe point me to the right solution - regardless of how
well I spoke their version of geek.
 
J

John Bokma

(e-mail address removed) (Tony McGuire) wrote in
What you guys don't seem to understand is that there some of out here
that don't have the *slightest* idea what the terms for a function
even are.

So how can we even search for them?

Read a book, really. I read through all Perl core functions, and now and
then do it again.
 
J

Joe Smith

Tony said:
I was hoping to coordinate everything with just perl, but it doesn't
seem to be up to this particular task.

Perl is up to the task.

Doing it via web request is the problem.

Programs invoked via a web server tend do so using the same credentials
as the webserver, not as the owner of the machine. The account that the
web service uses is not an administrator. That's one of the reasons
why a given program works when run from the command line and not from
the web service.
-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top