pid_t pid = 0;
pipe(pipefd); //create a pipe
pid = fork(); //span a child process
if (pid == 0)
// Child. Let's redirect its standard output to our pipe and replace process with tail
execl("/usr/bin/tail", "/usr/bin/tail", "-f", "path/to/your/file", (char*) NULL);
//Only parent gets here. Listen to what the tail says
output = fdopen(pipefd, "r");
while(fgets(line, sizeof(line), output)) //listen to what tail writes to its standard output
//if you need to kill the tail application, just kill it:
//or wait for the child process to terminate
waitpid(pid, &status, 0);
Do I need to close(pipefd) after I am done reading at the end? or right before kill?
Incidentally, if you use GLib, there's a pre-baked implementation of this with tons of features, plus it handles all the errors codes and so on. See developer.gnome.org/glib/2.30/glib-Spawning-Processes.html (disclaimer: I implemented this, though many years ago)
@havoc or libHX's HXproc_* family of functions, which does not use functions with that awkardly-many parameters as glib.
sure, just mentioning the library I know.
Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).