Execute commands on the server?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Execute commands on the server?

Post by toasty2 »

How can I execute commands on my Windows (XP x64 edition) Server. I was hoping to be able to run programs and maybe even stop them on my server. Like, for a remote management type thing..
How do I do this?
Thanks for any input you kind people may have.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Backtick syntax:

Code: Select all

echo `dir`;
Aslo: exec(), system(), popen(), proc_open()
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

So, exec can be used for launching command line programs or batch files, but Can it open a program with a GUI, like Notepad or Firefox?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Well, then it would launch the program on the server, but not on your client machine. In short, no.
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

Heh, i'm not stupid...I meant the Server machine lol.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Yes. It would work. The PHP script, however, would hang until the program shut itself back off.
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

How can I get it to completly not care about the program after it starts it?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Well, if you were on Unix, I would have pointed you to pcntl_fork, but to be blunt, at this point, PHP isn't the best language for this sort of task.
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

What happens if it "hangs"?
I was thinking of something liek this to run commands specified in a form submitted:

Code: Select all

<?php
exec ( $stuff );
?>
Last edited by toasty2 on Mon May 29, 2006 11:06 am, edited 2 times in total.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Well, the way PHP works, when you execute shell commands, it waits for the command to finish.

For GUIs, that means that as long as the screen is up, PHP will wait for the process to terminate.

Perhaps "hang" isn't the best word to describe it.
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

Code: Select all

<?php
exec("C:\Documents and Settings\Administrator\Desktop\winlister.exe");
?>
It doesnt do anything.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Double-escape the backslashes: "\\"
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

It doesnt work, it isnt starting the program.

Code: Select all

<?php
exec("C:\\Documents and Settings\\Administrator\\Desktop\\winlister.exe");
?>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

exec() isn't made for running GUI based applications. It just reads the response from STDOUT. Don;t use PHP for something like this - it's all kinds of wrong ;)
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

Could I tell php to run a batch file and have the batch file run a GUI based program?
Post Reply