Skip navigation
This discussion is archived

Launching server application, command line script, from a webpage

2045 Views 7 Replies Latest reply: Dec 7, 2007 11:41 AM by John Kinsella RSS
John Kinsella Level 1 Level 1 (135 points)
Currently Being Moderated
Nov 30, 2007 1:59 PM
Please forgive my ignorance here, I'm treading in water I haven't been in before. I want to build a very simple webpage with two buttons. A start button and a stop button. When you click on the Start button, it will run a specific command line command on the server itself (not the client.) And the stop button would do the same. I understand the security implications here, I'm simply investigating some options with a test server NOT hosting a production website. But, I'm pretty clueless where to start to make this happen. I could write some applescripts to do this, but cannot find any concrete answers whether ACGI scripts are supported by Leopard server. And I'm not sure how to do something like this with perl (I can't find any good examples I can follow to learn.) I'm hoping someone here might be able to help me figure this one out.
Intel Core 2 Duo, Mac OS X (10.5.1)
  • Roger Smith3 Level 6 Level 6 (13,475 points)
    The buttons just call a CGI script, it's pretty much that simple. The script should be placed (for the default server) in /Library/WebServer/CGI-Executables with read and execute permissions for owner, group and others. If the script is sending any output back the the client, it must start with a Content-type header. For html it would be:

    Content-type: text/html

    followed by 2 linefeeds.

    The URL would be http://127.0.0.1/cgi-bin/yourScriptName

    Any errors will be logged in /var/log/httpd/error_log.

    Have fun!

    Roger
    XServer, Mac OS X (10.4.7), XRAID, Xasperated
  • rsbennett00 Level 1 Level 1 (10 points)
    I think I'm trying to do the same as OP, place a start and stop button on a website for professors to start and stop podcast producer. I have cgi enabled and it works as I get the right output on the webpage from scripts like this:

    #!/bin/sh
    echo Content-type: text/html
    echo
    echo "<html>"
    echo "<head>"
    echo "<title>"
    echo "CGI Testing"
    echo "</title>"
    echo "</head>"
    echo "<body>"
    echo "Testing CGI script execution
    "
    podcast --server 'myosxserver.domain.com' --user 'podcaster' --pass 'password' --start 'TestCamera'
    echo "</body>"


    But it does not execute the command. I get an error in the log as such:


    [Mon Dec 03 16:53:35 2007] [error] [client x.x.x.x] /usr/lib/podcastproducer/agentcommon.rb:31:in `expand_path': couldn't find HOME environment -- expanding `~/Library/Preferences/com.apple.pcastagentd.plist' (ArgumentError)
    [Mon Dec 03 16:53:35 2007] [error] [client x.x.x.x] \tfrom /usr/lib/podcastproducer/agentcommon.rb:31:in `reload'
    [Mon Dec 03 16:53:35 2007] [error] [client x.x.x.x] \tfrom /usr/lib/podcastproducer/agentcommon.rb:37
    [Mon Dec 03 16:53:35 2007] [error] [client x.x.x.x] \tfrom /usr/bin/podcast:14:in `require'
    [Mon Dec 03 16:53:35 2007] [error] [client x.x.x.x] \tfrom /usr/bin/podcast:14

    The manual for podcast does not have an error number 14, error 31 says "upload server unavailable" which isn't true, and error 37 is unknown. The command above works perfectly in the terminal and I chmod'd the script for +x. What am I doing wrong? This should be very simple. Any help would be greatly appreciated. Thanks.
    Dozens, Mac OS X (10.5)
  • Roger Smith3 Level 6 Level 6 (13,475 points)
    One thing that you have to remember about CGI scripts is that they're executed by the webserver as the user that the webserver is running as (default www). The www user has very few privledges in case the webserver is hacked. The environment that the CGI script executes in is also different than the environment that a (regular) user works in. Just for fun, put this script on your webserver and run it:

    #!/bin/bash

    echo -e "Content-type: text/plain\n"
    printenv

    exit 0

    Roger
    XServer, Mac OS X (10.4.7), XRAID, Xasperated
  • rsbennett00 Level 1 Level 1 (10 points)
    I'm trying to grasp this but it's not sinking in. Placing your text above in place of mine results in the "Premature end of script headers: test.cgi" error. Placing the two middle lines above into the middle of my script leaving the headers intact results in:

    Testing CGI script execution
    Content-type: text/plain printenv echo

    Which doesn't seem to be what you expected. Perhaps my files are being saved wrong? I'm using textedit and saving them as Western (Mac OS Roman). I do understand that there is some invisible user that I can't find doing the scripting through the website so I've been doing chmod +x or chmod a=x to all my test files and I do get the proper output in the browser. Are my permissions wrong on agentcommon.rb or podcast ? I may try chmodding them.
    Dozens, Mac OS X (10.5)
  • rsbennett00 Level 1 Level 1 (10 points)
    I chmodded agentcommon.rb and /usr/bin/podcast with a a+x but it did not change the errors in the log.
    Dozens, Mac OS X (10.5)
  • Roger Smith3 Level 6 Level 6 (13,475 points)
    A .cgi file extension implies that the script will be in Perl. What I'd written was a BASH shell script. I usually set the privledges of my CGI scripts to rwx for owner, r-x for group and others. The scripts are owned by my unprivledged user account. One thing about ALL shell scripts is that the "#!" has to be the first two characters of the file followed by the absolute path to the interpreter that is actually going to be executing the script. If you're using scripts that you downloaded, make sure that the path is correct. The path will vary between *nixes.

    Now that you're moving into programming, you have to be sure to save these files as plain text. Formatting characters will drive you crazy in short order. TextEdit did have a save as plain text option, but I don't know if it still does. People on the boards have said that Text Wrangler is nice to use and saves reliable plain text. I don't know, I use vi. Emacs and pico are plain text editors that you can invoke from the shell. Pico is probably the simplest to figure out. vi is usually the default editor installed on all *nixes, and emacs is probably the most extensive (and complicated).

    The printenv command prints the environmental variables to stdout. What that means is that variables like your PATH variable will be printed to the screen if you run printenv in terninal, or if a CGI calls it, it will print to the client's browser. The difference between the two will quite possibly point out what's not working with podcaster.

    Roger
    XServer, Mac OS X (10.4.7), XRAID, Xasperated

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.