How to get tail -f results using an asynchronous script?

I have a PHP page in which I want to display the newly added lines to a log file during last 3 seconds. At the moment, I am using jQuery get function to run a PHP file, which in turns runs shell_exec.

I am using these scripts:

jQuery on main page:

setInterval(function(){
    $.get('tailer.php', function(data) {
        alert(data);
    });
}, 3000);

tailer.php:

<?php
    $result = shell_exec("timeout 3s sudo tail -f log.txt");
    echo $result;
?>

The problem is that when I run the tailer.php script from command line, it returns the lines as expected. But when running the same file using a browser, it doesn't stop loading and return nothing. So the script in the main PHP page does not alert anything.

I was able to narrow down the issue and noticed that when removing -f switch from tail command, everything is fine. The issue is only with -f switch. Could you please help?

First question

PHP engine executes php code when request comes and returns the output of php code when this execution is finished (You can still manually flush the output buffer by using flush function). It seems like your php code is blocked(waiting) when executing shell script.

Second question

I would suggest write a script running background just to put file changes into Redis and use ajax to query Redis for update. If you want pure php code, I can't find a resonable solution(PHP is not the best tool for long connection).

By the way, multiple shell script will be executed at the same time when multiple connection are established, which is not sufficient for such a file watch job.

Issue #385: Can't connect again after disconnection, tail -f /var/log/secure Aug 21 15:18:33 244 pluto[28233]: packet from pluto[ 28233]: "ios"[3] 182.18.19.212 #4: NAT-Traversal: Result using RFC 3947: 244 pluto[28233]: ERROR: asynchronous network error report on em1 for 15:45:38 244 pluto[28233]: "ios"[4] 182.18.19.212 #5: we have a cert and are� Thanks for your feedback about using grep and sed along with tail -f. @Dmitry, @Randy, I agree with you that screen is also another effecient way to view multiple log files on one terminal @Praveen, Thanks for sharing your tips. Running tail -f in the background may be OK for two files. Anything more than that, it may get little confusing. @Daniel,

When I first started writing web apps, I used this technique as well, JS - PHP - BASH. It's not the best way, you should consider using PHP (or Node) to handle all the server side stuff that bash is doing now.

I can't remember what the reason for the problem you have (and I had) is, but this solution worked for me:

In your tailer.php, use this code:

<?php
 exec("/bin/bash -c 'tail -f log.txt'", $output, $return_var);
 echo $output
?>

$return_var is the exit status of the bash script, and I think $output will hold the return values from the bash script.

Can you show me the response from the AJAX (found in the dev-tools Network section).

If there's nothing, can you switch echo $output for echo $return_var so we can see if the bash cmd of exiting true or false.

:-)

[Starpu-commits] r3386, scripts/buildbot/dashboard/index.tail.html Log: buildbot: script to upload -The latest nightly tarball successfully passing 'make distcheck' is -available at <a Copied: scripts/buildbot/dashboard/index.starpu.head.html (from rev 3379, border=1> +<tr> +<th>Date</th> <th>Revision</th> <th>Result</th>� Tip: tail will follow the file forever. To stop it, press CTRL + C. tail -f access.log | grep 24.10.160.10. This is a useful example of using tail and grep to selectively monitor a log file in real time. In this command, tail monitors the file access.log. It pipes access.log's final ten lines, and any new lines added, to the grep utility.

Don't know exactly why but the problem resolved after moving sudo to before timeout command. The final working tailer.php contents look like this:

<?php
    $result = shell_exec("sudo timeout 3s tail -f log.txt");
    echo $result;
?>

gist:4334956 � GitHub, Library; using TypeScriptServiceBridge; using TypeScriptServiceBridge; using ITextWriter Stdout { get; set; } [TypeScriptBridge ("readFile")] string ReadFile public enum ModuleGenTarget { Synchronous = 0, Asynchronous = 1, Local = 2, } @script.Instance : null); } [TypeScriptBridge ("logNode")] public void LogNode � So, these functions call the base http function but set the correct HTTP method and serialize the body for us.. The consuming code is now a little simpler! Wrap up. With some nice wrapper functions we can easily use fetch with async and await and TypeScript.

prometheus-metrics � GitHub Topics � GitHub, get http response time from Envoy proxy Backed by Redis and exposes to Prometheus Prometheus asynchronous metric exporter that simulates builds on your OCP Custom Script Result exporter (csr_exporter) for Prometheus An easy way to tail multiple application log files and expose a metrics endpoint for� Asynchronous JavaScript is a fairly advanced topic, and you are advised to work through JavaScript first steps and JavaScript building blocks modules before attempting this. If you are not familiar with the concept of asynchronous programming, you should definitely start with the General asynchronous programming concepts article

How to use Handlebar template in Sitecore, In Sitecore MVC as we all know how to bind data synchronously by using as json to view using handlebar template framework in asynchronous call. Following are the steps we have to follow: and return type would ben JSON result set by using Json() method script id=”application-declined-template”� Synchronous and asynchronous loading types explained. A web page consists of a head and a body. Everything in the body is rendered by the browser while the head is used to load external resources (such as scripts and style sheets) and to add meta data to the page.

Settings/desktop-4.0-windows.json 1.18.1, [-Results] \u003cList[Result]\u003e [[-RepositoryPath] \u003cstring\u003e] \ u003cstring[]\u003e] [-Asynchronous] [-Authentication \u003cstring\u003e] [- Credential \u003clong\u003e] [-TotalCount \u003clong\u003e] [-Tail \u003cint\ u003e] [\u003cCommonParameters\u003e] -Command \u003cstring[]\u003e [- Script� Trying to avoid asynchronous code and replacing it with synchronous code is almost always a bad idea in JavaScript because JavaScript only has a single thread (except when using Web Workers). That means the webpage will be unresponsive while the script is running.

Comments
  • Thank you, I changed my PHP script to <?php $result = shell_exec("/bin/bash -c 'tailer.sh > /dev/null 2>&1 &'"); echo $result; ?>. Now when running intermediate.php, it returns nothing. When I run the tailer.sh from command line, it returns the lines as expected. I tested removing " > /dev/null 2>&1" part as well. This time, the intermediate.php does not stop loading!
  • That's not the code I wrote though ;-) - don't set the exec command as a value to the $result var. Just use the $output var. I had edited the original post, maybe you saw the old answer? Sorry!
  • Yes :) I saw your first reply. I tested the new code too. But the result was the same. To see whats going in the background, I ran intermediate.php from command line to see the errors. The error was "PHP Notice: Array to string conversion in tailer.sh". So I used foreach($output as $line) { echo $line; } instead of simply echoing the $output. This time, it returned nothing!
  • Shoot, I think I made a mistake, try removing the > /dev/null 2>&1
  • Yeah I did that. It is now printing out the results when running the PHP file from command line. But when running from browser, it doesn't stop loading the page and shows nothing :(