T
T
tigra2018-12-18 23:11:17
linux
tigra, 2018-12-18 23:11:17

How to listen to a file in realtime?

Actually, the user data is written to the file using the utility. On the same server, I wrote an api that accepts logs. Is it possible to somehow implement that with each update of the file, or even simply, without a file, send data to the address of my api?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vman, 2018-12-19
@tigroid3

for a file, the simplest option would be to implement behavior similar to the tail utility, or call tail and take the output from it

$handle = popen("tail -f /path/to/my/logfile.log 2>&1", 'r');

while(!feof($handle)) {
    $buffer = fgets($handle);
    // здесь будет вызов API
    ob_flush();
    flush();
}
pclose($handle);

A
Alexey Cheremisin, 2018-12-19
@leahch

Well, at least the question was answered, but I'll add it. Linux has an inotify subsystem that allows you to monitor a file in real time - https://en.wikipedia.org/wiki/Inotify

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question