Click to See Complete Forum and Search --> : Want to execute script from Apache server web page button
dstaley
02-09-2003, 12:52 AM
Hello all,
I'd like to execute a few basic scripts from a web page hosted on my Linux server, just to kick off a preset Quake 3 Arena dedicated server routine.
How can I use an HTML button to execute the script rather than logging in to a SSH terminal window and executing it manually?
Thanks for any help I can get!
Im no expert at this but cant you just make a script(in perl?) that runs the script you want. Put it in your cgi-bin directory and link to it?
KilerCris
02-09-2003, 02:46 PM
if you just put it in a script and link to that, apache will sit around and wait for the script to exit. And depending on your settings I think hitting stop or closing the browser will terminate the script too. Have a button on a webpage submit to a php or perl script and hae the script execute the AT command. In perl it would be something like
#!/usr/bin/perl
`at -f your_shell_script.sh now`;
print "Content-type: text/plain\n\nServer Started";
in php you could have somethign like
<?
exec("at -f your_shell_script.sh now");
echo "Server Started";
?>
This will let your scripts to start the quake 3 server run asynchronously from the script on your website.
dstaley
02-09-2003, 09:06 PM
OK, I'll start learning how to program in PHP. I hadn't heard of it before starting to look for answers on this forum.
Any suggestions on the best place to start learning PHP? Right now I'll start with PHP.net.
Any further suggestions would be appreciated!
KilerCris
02-10-2003, 01:55 AM
PHP.net is an invaluable reference, but no good for learning. I suggest Webmonkey (http://hotwired.lycos.com/webmonkey/programming/php/) for a start and DevShed (http://devshed.com/Server_Side/PHP) for more advanced tutorials. There is also a very active php forum on devshed.
Oh, and if you don't already know HTML, you definatly need to know at least the basics to work with PHP. Webmonkey hasgood tutorials (http://hotwired.lycos.com/webmonkey/authoring/html_basics/) on that too.
dstaley
02-10-2003, 12:05 PM
I cannot thank you enough, sir. Thank you for getting me started in the right direction.
I've dinked around with HTML quite a bit, so I'm off to PHP land...
dstaley
02-10-2003, 11:32 PM
I don't seem to be successful at initiating Quake, but I have learned enough to get a PHP script running... the "server started" message works OK.
I've also tried substituting "passthru", "shellexec", and "system" with no luck.
I'm not sure what's wrong...
Any ideas?
KilerCris
02-11-2003, 12:26 AM
Try pasting the exact command you put into exec() into a shell and see if it works. Keep in mind that it will probably be executed as the user "apache".
dstaley
02-11-2003, 11:20 PM
If I execute the script under a user account, using sh ffa.sh, no problem. The script works fine.
There are two copies of the shell file on my system, one is located in the /root directory and one is in the /home/(username) directory for my other account. Might I need to copy the script file to some other location so that Apache can find it? Might there be another kind of problem?
I also tried editing my php script to the following:
<?php
exec("sh ffa.sh", $output, $return);
print "<p>Returned: $return</p>";
foreach ( $output as $out )
print "$out<br>";
echo "PHP script ended";
?>
Here's the result:
Returned: 127
PHP script ended
What does "127" mean, by chance?
KilerCris
02-12-2003, 02:37 AM
It doesn't matter where on your system the script is, just what it's permissions are and who owns it. Try giving user group and owner the execute permission. I don't know what the 127 means...thats the maximum value of a 1-byte signed *******
dstaley
02-13-2003, 08:02 PM
I've entered a vanilla script and HTML form from a book and have found that I cannot successfully pass form variable values from a form to a PHP script. I suspect my server setup isn't right somehow. The command phpinfo() does work; I get a page full of stuff that I will have to learn to understand.
Where are the best guides to checking/debugging my PHP install?
KilerCris
02-13-2003, 09:10 PM
Your book is probably showing you using register_globals ($form_var_name). This is a very bad practice to get used to. PHP now comes with register_globals disabled by default, but you can turn it on if you want (in /etc/php.ini or somewhere else maybe..search for 'php.ini' and restart apache for changes to take effect) It is preferable that you access form vars using track vars. I highly recommend you get into the habit of using the latter instead.
With register globals off, track vars are how you access the form vars. $HTTP_POST_VARS contains form vars submitted via the POST method (<form method='POST'>), $HTTP_GET_VARS contains form vars submitted via the GET method(default), and $HTTP_COOKIE_VARS contains all your cookies.
What I like to do is this at the top of my scripts:
$VARS = array_merge($HTTP_GET_VARS, $HTTP_POST_VARS);
and then you can access any form variables submitted to your script like this:
if(!empty($VARS['first_name'])) {
echo "First name: $VARS[first_name]";
}
In this example, there would have been a form field with name='first_name'.
Modorf
02-13-2003, 09:36 PM
if(!empty($VARS['first_name'])) {
echo "First name: $VARS[first_name]";
}
In this example, there would have been a form field with name='first_name'. [/B]
That should read ::
if(!empty($VARS['first_name'])) {
echo "First name: $VARS['first_name']";
}
'
KilerCris
02-13-2003, 09:44 PM
That will cause a parse error. You don't enclose the index of an array inside a double-quoted string.
dstaley
02-13-2003, 11:46 PM
WOOHOO!!!
Thanks, guys, at lest now I can pass the variables to the PHP script. I still can't execute the shell script, but I can start working on the config file generation.
This is a big breakthrough for me, so thanks again.
Any idea why I might not be able to execute the shell script? Might it be related to something related?
Thanks again!
KilerCris
02-14-2003, 03:57 PM
Well you need to isolate the problem.
First try and see if putting the exact same command into the shell yourself works.
If your putting any variables into the command, it might be a good idea to change the exec() or whatever to die() so you can see what exec is getting and make sure everything is in order.
If theres nothing wrong with the command your trying to execute, then you'll want to see if the problem might be in executing commands in general. Trying doing an exec() with a simple command like to make a directory or change a files permissions.
And the last thing I would recommend, which I think is the problem in your case, is file permissions. A PHP script runs from the same user that Apache is being run under, which is usually "apache" or "nobody". This means that not only does the shell script need to be accessible to apache, but also every file it runs and uses, and every file that the files in runs uses, are accessible to the apache user. You may want to try SUing to the user apache is running as and trying the command, or have your shell script or php script switch to a more privledged user to run your stuff.