• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 725
  • Last Modified:

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

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

0
ritztech
Asked:
ritztech
  • 7
  • 6
1 Solution
 
crazedsanityCommented:
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.
0
 
ritztechAuthor 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

0
 
crazedsanityCommented:
Hopefully another expert will have some insight into that; as I said, I'm not sure how to do that.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ritztechAuthor 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 ?
0
 
crazedsanityCommented:
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.
0
 
crazedsanityCommented:
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.
0
 
ritztechAuthor 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>";

?>
0
 
ritztechAuthor 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 ....
0
 
crazedsanityCommented:
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.
0
 
ritztechAuthor 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 ?
0
 
crazedsanityCommented:
Like I said in my original post, each file would be associated with a specific session.  This could be done by naming the file using the session ID.

I would just have the AJAX read the entire contents of the file each time; only reading new information can be tricky, and can require stopping/resuming on a specific line.  If I were doing this, I'd return the entire contents of the file, and the javascript would just update the page.  Each new ping request would just append to the end of the file for that user; if they wanted to send 100 pings, I'd just spawn a new process every 5 pings, so if they leave, I don't have to worry about the server continuing to run a process that won't be used.
0
 
ritztechAuthor Commented:
Closing Due to been 4 months
0
 
ritztechAuthor Commented:
Closing Due to been 4 months
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now