[Tutor] Linux tail -f multiple log files

Kent Johnson kent37 at tds.net
Wed Jan 7 03:10:50 CET 2009


On Tue, Jan 6, 2009 at 7:34 PM, David <david at abbottdavid.com> wrote:
> Hi,
> This works fine if I don't use the -f option;
> #!/usr/bin/python
> from subprocess import call
> from termcolor import colored
>
> def slog():
>    sudo = "sudo"
>    tail = "tail"
>    sfile = "/var/log/messages"
>    print colored("<syslog>", "blue")
>    call([sudo, tail, sfile])
>
>
> def alog():
>    sudo = "sudo"
>    tail = "tail"
>    afile = "/var/log/apache2/access_log"
>    print colored("<access_log>", "green")
>    call([sudo, tail, afile])
>
> def elog():
>    sudo = "sudo"
>    tail = "tail"
>    afile = "/var/log/apache2/error_log"
>    print colored("<error_log>", "red")
>    call([sudo, tail, afile])
>
> def main():
>    slog()
>    alog()
>    elog()
>
> if __name__ == "__main__":
>    main()
>
> Now if I do this to all the functions;
> def slog():
>    sudo = "sudo"
>    tail = "tail"
>    targ = "-f" # output appended data as the file grows
>    sfile = "/var/log/messages"
>    print colored("<syslog>", "blue")
>    call([sudo, tail, sfile])
>
> Only the first function will print to the screen.

subprocess.call() does not return until the spawned process
terminates. tail -f does not terminate so the later functions are
never called. You need to use a non-blocking operation to spawn the
subprocess.

Kent


More information about the Tutor mailing list