We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

jquery ajax xmlhttppost php (not sure what to use) run Linux Commands/Scripts in realtime. without waiting until output

ritztech
ritztech asked
on
Medium Priority
769 Views
Last Modified: 2012-05-11
Hola...

im trying to interface my DIV window when a user submits Data to send the Command and view results in Realtime (in this case Ping) every newline will show on page (if it needs to do it thatway)


ping 4.2.2.2 -c4

PING 4.2.2.2 (4.2.2.2) 56(84) bytes of data.
64 bytes from 4.2.2.2: icmp_seq=1 ttl=56 time=18.5 ms
64 bytes from 4.2.2.2: icmp_seq=2 ttl=56 time=18.6 ms
64 bytes from 4.2.2.2: icmp_seq=3 ttl=56 time=18.3 ms
64 bytes from 4.2.2.2: icmp_seq=4 ttl=56 time=18.3 ms

--- 4.2.2.2 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3000ms

most of my scripts /commands will have an Exit BUT im leary of leaving muliptle processes open which could cause alot of issues. so when window is closed close the attached process running..

Thanks
<form name="MyForm" action="response_normal.php" method="post" onsubmit="xmlhttpPost('response_ajax.php', 'MyForm', 'RESPONSE', '<img src=\'pleasewait.gif\'>'); return false;">

      <div id="radio">
            <p>
              <input type="radio" id="radio1" name="site"  value="radio1" />              <label for="radio1">Script1</label>
              <input type="radio" id="radio2" name="site"  value="radio2"  />              <label for="radio2">Script2</label>
              <input type="radio" id="radio3" name="site"  value="radio3" />              <label for="radio3">Script3</label>



  <p><strong>Enter  Site:</strong>
      <input type="text" name="search" id="search" />
      <input type="submit" value="Search Site" name="submit" />
     


 <div id="RESPONSE" style="width: 400px">






this is the response_ajax.php

?php
$radio=$_POST['site'];
$search=$_POST['search'];
if ($radio == radio1)
{
//   $search1 = escapeshellcmd($_REQUEST['search']);
//      passthru('/usr/local/bin/script1 -w ' .$search);
 $command=('/usr/local/bin/script1 -w ' .$search);
$output=shell_exec($command." 2>&1");  //system call
print "$output";

}
elseif ($radio == radio2)
{
//      shell_exec('/usr/local/bin/script2' .$search);
passthru("ping 4.2.2.2 -c4");      

}
elseif ($radio == radio3)

Open in new window

Comment
Watch Question

I'm not sure how the process would get closed if the browser window is closed.  That has always seemed like a fairly complicated system, and is heavily reliant on javascript being able to detect that the window/browser is being closed and have time to act; in my experience, one should make sure there are fail-safes in place in the event that the browser is closed without a chance to notify the script.

That being said, my first thought would be to have the process write to a temporary file that is linked to the session (so only that user sees the output).  The PHP that is called from AJAX would simply read the entire contents of the file; the AJAX would then simply replace the old output with the updated one; to the user, it should look fairly fluid, especially if the polling interval is quick enough.

A more elegant solution might be HTTP Streaming.  I've glossed over this article [ http://ajaxpatterns.org/HTTP_Streaming ], and it seems like it might be a bit more fluid.

Author

Commented:
what if the proccess runs until finished as my scripts will have a closing but im not sure what kind of code i can do this if i have a <DIV>

and xmlhttppost

Hopefully another expert will have some insight into that; as I said, I'm not sure how to do that.

Author

Commented:
Anyone ? ??? ? ?


what about proc_open() with some sort of fifo  but attach to an active running process to run the script or

ping 4.2.2.2 -c10 command ?
This is a bit more complex of a problem than you might think.  Generally, PHP running on the web should not be able to spawn other processes for safety issues.  The big part, however, is that it isn't a stateful process: a user makes a request, the server returns the page, and that's it.  Ajax makes it seem stateful, but that's just because it can make automated, frequent requests to the server.

With a stateful application, like a client-side Windows program, is actively watching for changes; a stateless application, such as a web application or web page, passively waits for users to request something.

In other words, the program (whatever starts the "ping" process) has to monitor something and wait for a *lack* of updates to see that the client is no longer listening (i.e. the browser window closed).  The web page can start up, and use AJAX to start the process in the background.  Every second it can then "check in" (make/update an entry in a database) to show that it is still alive.  The background process which spawned the "ping" process would have to monitor that record: once it becomes more than 2 seconds old, it can safely assume that the browser window has closed.  In the event that the record disappears (i.e. an AJAX event deleted the record with the window closed), it can terminate the process as well.

Does that make sense?  A script has to fork (or otherwise spawn a process) which it can monitor and has the capability of terminating; that's the part that monitors that record to see if it has become stale or disappeared.

One system you might want to check out is "cs-multithread" [ http://sourceforge.net/projects/cs-multithread/ ](it is a multi-process system, since PHP doesn't multi-thread; I named it before I realized the difference).  This system is built to open processes and monitor them, and may be what you're looking for.
Oh, there isn't a download for the project, so you'd have to check it out from Subversion: https://cs-multithread.svn.sourceforge.net/svnroot/cs-multithread/trunk/0.2/

The system is dependent upon cs-content [ https://cs-content.svn.sourceforge.net/svnroot/cs-content ] and cs-webapplibs [ https://cs-webapplibs.svn.sourceforge.net/svnroot/cs-webapplibs ].  It takes a bit of setup.

I would again recommend safe-guards to handle the situation where the browser was closed.  For instance, if the application wants an unending ping to happen, purposely limit the ping command to run 5 times; the web application can just request that the ping start again; if the process starts and the browser window closes, the process will automatically die-off.

Author

Commented:
shoot im not able to get this to install but i did find someting


I cant install anything ... i can use anything that doesnt require any installation of packages.

though i did come across reading about

popen()

or

proc_open()

heres what i found it SEMI works

but the problem here is it runs the comannd 60 times ...

i wanted to make the timelimit for this 60 seconds view the output and keep seeing new lines as the ouput of the single command Updates ..


<?php
    ob_start("ob_gzhandler");
    $foo = "<!doctype html><html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"></head><body>";
    $foo .= str_repeat("\r\n",256);
    echo $foo;
    ob_flush();
    flush();
    for($i = 0; $i < 60; $i ++) {
        passthru(ping 4.2.2.2 -c4);
        ob_flush();
        flush();
        sleep(1);
    }
    echo "</body></html>";

?>

Author

Commented:
basically some of my scripts lasts for 1 minute Max and if i limit that i thought it will loop through the command ouptut once everysecond to chkec on the output ....
It executes 60 times because every call to passthru() spawns a new process.  Run the process once, and write the output to a file; once every second, you can then get the output of that file... or you can turn off output buffering, run the command, and let it output in it's own time (assuming that this is done within the page that is rendering and not retrieved through some AJAX call.

Author

Commented:
how could i get the process to output to a file if thers like 20 users issuing the same thing can i do a variable so its set to the current user and loop through the varaible contents ?
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Closing Due to been 4 months

Author

Commented:
Closing Due to been 4 months
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.