Execute Windows program from Perl script (??)

Z

Zebee Johnstone

In comp.lang.perl.misc on 28 Aug 2004 11:52:38 -0700
Tony McGuire said:
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
to the answers you get.

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

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
knows nothing at all about your particular circumstances can understand
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
together, which might help you a lot.

Zebee
 
A

Anno Siegel

John Bokma said:
but.. but.. it does, look:

perl -e "print 100 * 0.5";

That diminishes, but doesn't subtract: 100 * 0.5 isn't 100 - 0.5. Make
that 100 * 100/101, which is 100 - 100/101.

Anno
 
J

John Bokma

(e-mail address removed)-berlin.de (Anno Siegel) wrote in @mamenchi.zrz.TU-Berlin.DE:
That diminishes, but doesn't subtract: 100 * 0.5

It substracts 50 here :-D.
 
T

Tintin

Tony McGuire said:
(e-mail address removed) (Tony McGuire)
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.
 
T

Tony McGuire

Well, I'm going to take the easy way out.

I'll just write something to a file with perl, and have the scheduler
watch for that file to be modified.

Then the scheduler program can start the external program.

Thank you to everyone who participated. I really do appreciate the
feedback, regardless of whether it helped or not.
 
T

Tony McGuire

Zebee Johnstone said:
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.
 
T

Tony McGuire

Tintin said:
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'
answer might be.

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

Brian McCauley

Tony said:
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
in this thread.
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
instead.
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.
I don't know any of those languages, so I have no idea what the 'same'
answer might be.

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

Tony McGuire

Joe Smith said:
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.
 
C

Caj Zell

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?

I browse this ng quite often and from many of the threads I've seen, I
have grown so conscious of what I would be asking (and thinking about
whether it is stupid, or not) that I very seldom put a question here.
These guys are probably right (at least to some extent), but it cannot
be the meaning that a stupid perl newbie like myself feels so
intimidated by the responses that he dare not pose a question.

The old saying "There are no stupid questions" does not seem to have
gained much ground here.

Caj Zell
 
G

Gunnar Hjalmarsson

Tony said:
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.
 
Z

Zebee Johnstone

In comp.lang.perl.misc on 29 Aug 2004 10:58:52 -0700
Caj Zell said:
I browse this ng quite often and from many of the threads I've seen, I
have grown so conscious of what I would be asking (and thinking about
whether it is stupid, or not) that I very seldom put a question here.
These guys are probably right (at least to some extent), but it cannot
be the meaning that a stupid perl newbie like myself feels so
intimidated by the responses that he dare not pose a question.

I think that attitude of many perlprogs is why learn.perl.org was
started. The people there *want* folk to program in perl and are
willing to help them learn to do so.

That said... one useful thing a forum like this does is make you thnk
about what you ask, and that act of thinking can mean you get the
answer.

I've pondered a couple of questions, started writing them, realised I
needed more info or to test something, and found a workable solution.

It's made me work, not take the easy way out.

I suspect most folk starting out would be better off at learn.perl.org

Zebee
 
T

Tony McGuire

(e-mail address removed) (Caj Zell) wrote

Usenet is known for its high jerk-vs-helper ratio.

We still wade in, hoping to find a diamond amongst the coal.
 
T

Tintin

Tony McGuire said:
I don't know any of those languages, so I have no idea what the 'same'
answer might be.

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
chasing your tail.
 
Z

Zebee Johnstone

In comp.lang.perl.misc on 30 Aug 2004 09:31:21 -0700
Randal L. Schwartz said:
Zebee> I think that attitude of many perlprogs is why learn.perl.org was
Zebee> started. The people there *want* folk to program in perl and are
Zebee> willing to help them learn to do so.

And here too. I dare you to show me one posting where a well-reasoned
question is posted with relevant info that isn't answered in a FAQ, that
doesn't get a serious answer in response.

I don't think that's really the point.

It might be if you are newsgroup experienced, and have a good idea what
you want and how to ask it.

But I think most people are lost and really dont' understand what to ask
yet. If they see 10 posts being bastards for every reasonable one, then
they are going to think "people are bastards", not reason out why one
gets one style and one gets another.

Intellectually lazy quite probably, human nature definitely.

Whereas a forum that educates politely is probably going to get more of
it across. Most people don't have the time or energy to do that, so a
place which has as its *reason* that style of education is a good thing.

I've been around long enough that I know the drill, and enough of a
thick skin to be able to say "I don't understand, please explain more".
It is possible that bitchiness will teach people that, I dunno it's so.

I would certainly prefer that everyone works hard themselves before
getting other people to do it for them, but I am also aware that many
people simply don't have the intellectual tools, they've never really
been taught *how* to think. I dunno clpm is the right place for them to
learn that, learn.perl.org, with the ethos of doing just that, might
well be.

It's certainly a less intimidating place to ask simple questions that
you can't answer for yourself even after reading the docs. ONe of the
hardest thing in the world, especially when confronted by heaps of
people who clearly know it all more or less and toss around line noise
they say is perlcode, is to say you don't understand some more basic
things, especially if you don't quite see what you don't know.

I'd rather people got help than not, pretty much.

Zebee
 
Z

Zebee Johnstone

In comp.lang.perl.misc on 31 Aug 2004 07:43:37 -0700
Randal L. Schwartz said:
Zebee> But I think most people are lost and really dont' understand what to ask
Zebee> yet.

Then they should find a help desk. Usenet is not a help desk.

WHich is what learn.perl.org *is* more or less. WHich is why I'm having
trouble working out why you are opposed to telling people it's there and
go to it.

After all, where are non-corporate people going to find a help desk
about perl if the community doesn't provide it?
Neither are the mailing lists that you keep waving about at
learn.perl.org, which have less noise only because they also have less
signal. If those mailing lists handled as much volume as this
newsgroup, the noise would go up significantly.

Noise isn't the problem, it's how the noise is handled.And the tribalism is precisely the problem that they are trying to
solve. I hope they can continue to be successful in it.

Zebee
 
I

Ian Wilson

Tony said:
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

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

Tony McGuire

Ian Wilson said:
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
sending the 'reload'.

Ian, thank you.
 
I

Ian Wilson

Tony said:
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
sending the 'reload'.

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

Brian McCauley

Ian said:
Actually, I fell victim to Xah Lee's Unixism there.

There may be some Win32 API for service restart you could invoke
using the appropriate Perl modules for Win32.

I don't think there is for restart but there is for stop and start (as
mentioned elsewhere in this thread).
 

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
474,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top