# Execute Windows program from Perl script (??) - Page 2

•  Subject
• Author
• Posted on

## Re: Execute Windows program from Perl script (??)

anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote in news:cgqtpn$gig$3
@mamenchi.zrz.TU-Berlin.DE:

>>
>> > Complaining that the multiplication operator doesn't substract is kind
>> > of beside the point.
>>
>> but.. but.. it does, look:
>>
>> perl -e "print 100 * 0.5";
>
> That diminishes, but doesn't subtract: 100 * 0.5

It substracts 50 here :-D.

--
John                               MexIT: http://johnbokma.com/mexit/
personal page:       http://johnbokma.com/
Experienced programmer available:     http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

## Re: Execute Windows program from Perl script (??)

Tony McGuire wrote:

>
>
>>
>>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.

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?

## Re: Execute Windows program from Perl script (??)

Tony McGuire wrote:

> 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

## Re: Execute Windows program from Perl script (??)

> 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

And that explains the main problem I've been seeing.

So I'm just going to use the perl script (or CGI script, whatever the
@#$$it is), kicked off from a web browser POST, to write to a file after modifying the Apache conf file. Then the scheduler program will take care of restarting Apache. It'd sure have been nice to work this from a single location, though. ## Re: Execute Windows program from Perl script (??) Tony McGuire wrote: > So I'm just going to use the perl script (or CGI script, whatever > the @#$$ it is),

You seem not to be interested in understanding the distinction between
running a Perl program from the command line and running it as a CGI
process. You'll keep being unhappy with CGI - whichever programming
language was used for writing the program - until you acknowledge and
learn the differences between those ways of invoking a program.

--
Email: http://www.gunnar.cc/cgi-bin/contact.pl

## Re: Execute Windows program from Perl script (??)

> 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.

Certainly possible, but you'd have to take webserver permissions into
consideration.  Also, if all you are doing is modifying the Apache config
file, you'd be better off doing the mods directly with Perl (webserver
permissions aside).

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

It can be done, but ask yourself the question, "How would I do this in C,
VB, Fortran etc"?  The answer will be the same.

## Re: Execute Windows program from Perl script (??)

> Certainly possible, but you'd have to take webserver permissions into
> consideration.  Also, if all you are doing is modifying the Apache config
> file, you'd be better off doing the mods directly with Perl (webserver
> permissions aside).

I also need to restart Apache.  And since the script doing this is
executing under Apache, I don't think it'd work since no one has been
able to help me figure out how to do an execute and continue.

And if I have to do some of the work externally, why bother to learn
how to do it in a language as archane as Perl?  Much easier to use a
modern program that comes with actual examples of how to accomplish
things.

> It can be done, but ask yourself the question, "How would I do this in C,
> VB, Fortran etc"?  The answer will be the same.

I don't know any of those languages, so I have no idea what the 'same'

And thanks, Tintin.  I do appreciate your time and wisdom, although
the tone of this message would probably give you a different
impression.

## Re: Execute Windows program from Perl script (??)

Tony McGuire wrote:

>
>
>>Certainly possible, but you'd have to take webserver permissions into
>>consideration.  Also, if all you are doing is modifying the Apache config
>>file, you'd be better off doing the mods directly with Perl (webserver
>>permissions aside).
>
>
> I also need to restart Apache.

You mean something like:

system("net restart apache");

Or if you prefer you could do it more directly in Perl using StopService
and StartService from the Win32::Service module.  Before you turn round
and say "how was I supposed to know about Win32::Service?" I should
point out that I didn't know about it either when I started writing this
response.  All I knew was that there were Win32::* modules that provided
Perl interfaces to the Windows API - something I'd pointed out earlier

>  And since the script doing this is
> executing under Apache, I don't think it'd work since no one has been
> able to help me figure out how to do an execute and continue.

Win32::Spawn(COMMAND, ARGS, PID)
[CORE] Spawns a new process using the supplied COMMAND, passing in
arguments in the string ARGS. The pid of the new process is stored in
PID. This function is deprecated. Please use the Win32::Process module

> And if I have to do some of the work externally, why bother to learn
> how to do it in a language as archane as Perl?  Much easier to use a
> modern program that comes with actual examples of how to accomplish
> things.

Yes, if you can find a ready written program that does exactly what you
want this is in any particular case often easier than learing to program
in a general purpose programming language.  If you do know a general
purpose programming language and you are happy that you can achieve
everything you want with it then there is no need to learn another
(unless of course you want to make a living as a programmer in which
case knowing at least a half a dozen general purpose programming
languages is a good idea).  Be aware however that when choosing a
general purpose prgramming language to learn, choosing one in which you
happen to find examples that address the first problem you encountered
is probably not a good basis.

>>It can be done, but ask yourself the question, "How would I do this in C,
>>VB, Fortran etc"?  The answer will be the same.
>
>
> I don't know any of those languages, so I have no idea what the 'same'

Well, if you know any general purpose languages then substitute their
names into that sentence.

If you can't program at all then we're back to the choice of learning to
program or hoping that someone else has written a program that does what
you want.

## Re: Execute Windows program from Perl script (??)

> > It can be done, but ask yourself the question, "How would I do this in
C,
> > VB, Fortran etc"?  The answer will be the same.
>
> I don't know any of those languages, so I have no idea what the 'same'

The point of my question is that the answer is language dependent, ie:  they
all would require an equivalent of a system call and the appropriate
permissions set on the requires dirs/files.  The point of the retortical
question (and it is asked quite often) is to make the OP think about
partitioning question.  Too often, people don't understand how the various
technologies work together in the browser/web server/CGI/Perl world.  The
first skill to learn in problem solving is to isolate where the problem
exists.  This can take some time and experience to learn, but it is
certainly an essential skill to learn early on, otherwise you'll end up

## Re: Execute Windows program from Perl script (??)

Tony McGuire wrote:

> 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).
>
>
> 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

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' <backspace> 'a' 'r'.

>  The web page I post from just sits there as if the perl
> were executing.

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).

## Re: Execute Windows program from Perl script (??)

>
> >  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).
>

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.

## Re: Execute Windows program from Perl script (??)

Tony McGuire wrote:
>>
>>>  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).
>
> 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

use warnings; use strict;

> 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

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

> 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

## Re: Execute Windows program from Perl script (??)

Tony McGuire wrote:
>> 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.

## Re: Execute Windows program from Perl script (??)

> 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.

--
John                               MexIT: http://johnbokma.com/mexit/
personal page:       http://johnbokma.com/
Experienced programmer available:     http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

## Re: Execute Windows program from Perl script (??)

In comp.lang.perl.misc on 28 Aug 2004 11:52:38 -0700
>
> I've tried to explain what I am trying to do.

YUp, and you are getting a rather nasty education in how to do that
explanation...

Start again.  THis time, explain what the problem is you are trying to
solve, and include all the detail you can.

You know what you want to do, but you don't know what bits are important
and what bits aren't.  You don't know what things you are doing have an
effect.  You didn't, for example, realise that doing it from a web page
might be important, so you didn't mention it.  THere might be other
things that are important, but you don't know what they are, and they
might seem unimportant to you.  But they might make all the difference

So, start with a description of the problem you are trying to solve.
NOt the perl one, the people one.  When you sit down at the computer,
what do you want to do?  What's the final outcome?  For example, is it
that you want to be able to edit a file on the webserver?

Lets assume that's it, that you want to fire up notepad on a windows
server and edit a file that lives on that server.  So your question
would be something like:

I have a Windows XP machine running IIS 5.0.  There are files
on a website on that machine that I want to edit using something

What I want to happen is I open a webpage, push a button on the
page, and up pops a textpad with the file in it to edit.

So I started by having a perl script that has
<insert full script here, not just the line you are having
truble with, and be sure it includes use strict and use warnings>

in it, and calling that with  http://my.server/perlscript.pl but
nothing appears on the page, and I get a lot of cmd processes
on the server box which I can't kill.

I think the problem is the open statement with the spaces.

YOu will probably get a bunch of people calling you an idiot for trying
to do what you are doing, welcome to usenet.  You will also probably get
someone pointing you to a book or tutorial, someone suggesting you use a
textarea in the web page, someone explaining what's going on, someone
correcting your code showing why it didn't work, and so on.  And in all
that, you will almost certainly get enough hints - no matter how badly
expressed - that you can go what you want to do.

Remember that people here are helping you out of the goodness of their
hearts.  They owe you *nothing*.  You hope like hell they've had a good
day and are willing to spend their own time and effort on a total
stranger who, 90% of the time, won't even say thanks.  You can increase
your chances of someone having that good day by giving them the
information they need to work with.  Which is a coherent description of
the big problem you are trying to solve as well as the small code one
that you are focused on right now.  And do it in a way that someone who
what's going on.

Oh, you will also get brownie points for asking what tutorials or
documentation deals with what you are trying to do.  Because there
probably is at least one, and it might save a lot of heartache to read
it.

In your case, you might want to hop over to http://learn.perl.org/ and
check out the online library, there are books there that will help a lot
with terminology and especially how web and programs and such fit

Zebee

## Re: Execute Windows program from Perl script (??)

> You know what you want to do, but you don't know what bits are important
> and what bits aren't.  You don't know what things you are doing have an
> effect.  You didn't, for example, realise that doing it from a web page
> might be important, so you didn't mention it.  THere might be other
> things that are important, but you don't know what they are, and they
> might seem unimportant to you.  But they might make all the difference
> to the answers you get.
>

I'm a newbie to Perl, not newsgroups.  I've been running a news server
myself for over 2 years, and I've been on the 'answering' side a
couple thousand times.

Yes, I probably should have explained the environment better.  And
yes, I should have known better.

But you don't know what you don't know.

This thread DID help me to understand those I reply to a bit better.

## Re: Execute Windows program from Perl script (??)

Tony McGuire wrote:
> What I keep getting is correction of capitalization, correction of use
> of terms,
....
> 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.

s/this stuff/forking/;
s/loooong/very long/;
s/us/we/;
s/didn't need to/need not/;
s/NG/CLPM/;
s/clic/clique/;

;-)

From the original posting ...

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

I think someone has already suggested fork() and identified some of the
the limitations (e.g. the parent should be coded to eventually reap the
child's status).

I'm sure I've seen an example in the Camel book.

'perldoc -q fork' describes starting up a child process that is
completely disassociated from the parent.

However,

If I recall correctly, you want to invoke textpad to edit apache's
config and then get Apache to reload its config.

I'd find it a lot easier to use perl to

* write a new httpd.conf.new,
* rename httpd.conf httpd.conf.old
* rename httpd.conf.new httpd.conf
* get Apache's process ID (hit PID file)
* send a 'reload your config' signal (HUP) to Apache. (perldoc -f kill)

I suspect this would be an order of magnitude simpler.

## Re: Execute Windows program from Perl script (??)

>
> If I recall correctly, you want to invoke textpad to edit apache's
> config and then get Apache to reload its config.
>
> I'd find it a lot easier to use perl to
>
> * read the config file,
> * write a new httpd.conf.new,
> * rename httpd.conf httpd.conf.old
> * rename httpd.conf.new httpd.conf
> * get Apache's process ID (hit PID file)
> * send a 'reload your config' signal (HUP) to Apache. (perldoc -f kill)
>
> I suspect this would be an order of magnitude simpler.

Now *this* is the type of thing I was hoping for.

I've already gotten to the point of modifying the httpd.conf file with
Perl.

But I'm using an outside scheduler to reload Apache.  It'd be smoother
to do it directly in the Perl script; much preferred.

So now I'm only left with figuring out retrieving a process ID, and

Ian, thank you.

## Re: Execute Windows program from Perl script (??)

Tony McGuire wrote:

>
>>If I recall correctly, you want to invoke textpad to edit apache's
>>config and then get Apache to reload its config.
>>
>>I'd find it a lot easier to use perl to
>>
>>* write a new httpd.conf.new,
>>* rename httpd.conf httpd.conf.old
>>* rename httpd.conf.new httpd.conf
>>* get Apache's process ID (hit PID file)
>>* send a 'reload your config' signal (HUP) to Apache. (perldoc -f kill)
>>
>>I suspect this would be an order of magnitude simpler.
>
>
> Now *this* is the type of thing I was hoping for.
>
> I've already gotten to the point of modifying the httpd.conf file with
> Perl.
>
> But I'm using an outside scheduler to reload Apache.  It'd be smoother
> to do it directly in the Perl script; much preferred.
>
> So now I'm only left with figuring out retrieving a process ID, and

Actually, I fell victim to Xah Lee's Unixism there. Whilst there is a
lot of interesting stuff in /var/run on a Linux system, on a Windows
system things are ... different. You could perhaps get the PID from
apache's error log but I've no idea if signals work in Win32. There may
be some Win32 API for service restart you could invoke using the
appropriate Perl modules for Win32.

I'd look at http://httpd.apache.org/docs/windows.html , especially the
bit at the end. Then perldoc -f system or perldoc -f exec.

## Re: Execute Windows program from Perl script (??)

Ian Wilson wrote:

> Tony McGuire wrote:
>
>>
>>> * send a 'reload your config' signal (HUP) to Apache. (perldoc -f kill)
>>>
>> Now *this* is the type of thing I was hoping for.
>>
>> So now I'm only left with figuring out retrieving a process ID, and