Sponsored Content
Top Forums Shell Programming and Scripting [solved] How to see log in real time? Post 302827757 by Tieso on Sunday 30th of June 2013 03:31:43 PM
Old 06-30-2013
Quote:
Originally Posted by Don Cragun
It looks like python is buffering output so there is no external way to view the results until python flushes its output buffer. Normally this will happen when the buffer fills and when python closes the output file. If you have permission to change the python source file (example.py), consider adding flush() calls to appropriate spots in the code so tail will be able to see data written to the file after significant updates are written.
Thank you very much, this is the problem!
It can be solved adding a flush() method after the prints you want to flush:
Code:
import sys
sys.stdout.flush()

Adding a flush() class like this:
Code:
class flushfile(object):
    def __init__(self, f):
        self.f = f
    def write(self, x):
        self.f.write(x)
        self.f.flush()

import sys
sys.stdout = flushfile(sys.stdout)

Or more easily adding -u option when invoke python:
Code:
python -u example.py >> log &

then with tail command see any print in real time:
Code:
tail -f logfile

Thanks a lot
Solved

Last edited by Tieso; 06-30-2013 at 04:48 PM.. Reason: I don't see where edit thread title for change to Solved
 

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

log users real time

hi.... how i can configurator a log file on real time....on unix solaris.... thanks a lot.... Best Regards... (3 Replies)
Discussion started by: chanfle
3 Replies

2. Shell Programming and Scripting

Real time log file redirect

Hi all, i would like to write the shell script program, it can monitor the access_log "real time" when the access_log writing the line contain "abcdef" the program will be "COPY" this line into a file named "abcdef.txt", do the same thing if the contain "123456" "COPY" it into a file named... (3 Replies)
Discussion started by: eric_wong_ch
3 Replies

3. Shell Programming and Scripting

Perl or Shell script to read a transaction log in real time

Hello, I have a Apache webserver running on RedHat. Its primary function is a proxy server for users accessing the internet. I have a transaction log that logs every transactions of every users. For users trying to access certain sites/content the transactions goes into a 302 redirect loop and... (2 Replies)
Discussion started by: bruno406
2 Replies

4. Shell Programming and Scripting

shell script to replicate the log files from one location to another in real time

Hi, On the server, we have app log files in this location /app/logs/error.log On the same server, in a real time, we would like to replicate that into /var/ directory. if someone has already done this, please share the script. Thanks in advance. (4 Replies)
Discussion started by: lookinginfo
4 Replies

5. HP-UX

HP-UX real time audit log writing

Hey all, I have a problem I was hoping to get some help on. So I have my two auditfiles, audfile1 and audfile2 that can be written to, I want to have the text version of them write to an NFS mount that I have set up. So i already know that i can do .secure/etc/audsp audfile1 > //nfsmount/folder/... (5 Replies)
Discussion started by: CleverRiver6
5 Replies

6. Shell Programming and Scripting

Archiving or removing few data from log file in real time

Hi, I have a log file that gets updated every second. Currently the size has grown to 20+ GB. I need to have a command/script, that will try to get the actual size of the file and will remove 50% of the data that are in the log file. I don't mind removing the data as the size has grown to huge... (8 Replies)
Discussion started by: Souvik Patra
8 Replies

7. UNIX for Advanced & Expert Users

How to read a fast written log file at Real time speed?

Hello All, I am building a real time parser for a log file in my application. The log file is continuously written at a very fast pace and gets rolled over every 10 minutes. I have measured the speed and observed that around 1000 lines are written to it every second, each line about 30-40... (7 Replies)
Discussion started by: cool.aquarian
7 Replies

8. Shell Programming and Scripting

Log all the commands input by user at real time in /var/log/messages

Below is my script to log all the command input by any user to /var/log/messages. But I cant achieve the desired output that i want. PLease see below. function log2syslog { declare COMMAND COMMAND=$(fc -ln -0) logger -p local1.notice -t bash -i -- "$USER:$COMMAND" } trap... (12 Replies)
Discussion started by: invinzin21
12 Replies
lfc_python(3)							 Python Reference						     lfc_python(3)

NAME
lfc - Python interface to the LFC lfcthr - Thread enabled version of Python interface to the LFC SYNOPSIS
import lfc import lfcthr lfcthr.init() DESCRIPTION
The lfc module permits you to access the LFC client interface from python programs. The lfc module is a swig wrapping of the standard C interface. For detailed descriptions of each function see the individual man page of each function. The lfcthr module is a version of the lfc module supporting multi-threaded Python clients. Its usage is similar to the usage of the lfc module except the obligatory initialisation call lfcthr.init() in the main program before threads are started. There follows a series of examples of how to use selected functions and how to retrieve the information returned by them: Examples are finding the GUID of an existing entry, listing the replicas of a given GUID and setting and retrieving the comment associated with an entry. EXAMPLE
#!/usr/bin/python import sys import lfc """ # stat an existing entry in the LFC and print the GUID """ name = "/grid/dteam/my.test" stat = lfc.lfc_filestatg() res = lfc.lfc_statg(name,"",stat) if res == 0: guid = stat.guid print "The GUID for " + name + " is " + guid else: err_num = lfc.cvar.serrno err_string = lfc.sstrerror(err_num) print "There was an error while looking for " + name + ": Error " + str(err_num) + " (" + err_string + ")" sys.exit(1) EXAMPLE
#!/usr/bin/python import lfc """ # list the replicas of a given entry, starting from the GUID """ guid = "6a3164e0-a4d7-4abe-9f76-e3b8882735d1" listp = lfc.lfc_list() flag = lfc.CNS_LIST_BEGIN print "Listing replicas for GUID " + guid num_replicas=0 while(1): res = lfc.lfc_listreplica("",guid,flag,listp) flag = lfc.CNS_LIST_CONTINUE if res == None: break else: rep_name = res.sfn print "Replica: " + rep_name num_replicas = num_replicas + 1 lfc.lfc_listreplica("",guid,lfc.CNS_LIST_END,listp) print "Found " + str(num_replicas) + " replica(s)" EXAMPLE
#!/usr/bin/python import sys import lfc import re """ # setting and retrieving a comment on a file """ file = "/grid/dteam/my.test" comment = "MyComment" res = lfc.lfc_setcomment(file,comment) if res != 0: err_num = lfc.cvar.serrno err_string = lfc.sstrerror(err_num) print "Problem while setting comment for " + file + ": Error " + str(err_num) + " (" + err_string + ")" sys.exit(1) buffer="" for i in range(0,lfc.CA_MAXCOMMENTLEN+1): buffer=buffer + " " res = lfc.lfc_getcomment(file,buffer) if res != 0: err_num = lfc.cvar.serrno err_string = lfc.sstrerror(err_num) print "Problem while reading the comment for " + file + ": Error " + str(err_num) + " (" + err_string + ")" sys.exit(1) r = re.compile("(.*?) ", re.DOTALL) comment = r.findall(buffer)[0] print "Read back comment " + comment EXAMPLE
#!/usr/bin/python """ # Using the lfc_readdirxr method """ import sys import lfc name = "/grid/dteam/my.test" dir = lfc.lfc_opendirg(name,"") if (dir == None) or (dir == 0): err_num = lfc.cvar.serrno err_string = lfc.sstrerror(err_num) print "Error while looking for " + name + ": Error " + str(err_num) + " (" + err_string + ")" sys.exit(1) while 1: read_pt = lfc.lfc_readdirxr(dir,"") if (read_pt == None) or (read_pt == 0): break entry, list = read_pt print entry.d_name try: for i in range(len(list)): print " ==> %s" % list[i].sfn except TypeError, x: print " ==> None" lfc.lfc_closedir(dir) EXAMPLE
#!/usr/bin/python import lfc """ # Using the lfc_getlinks method """ result, list = lfc.lfc_getlinks("/grid/dteam/antotests/extratests/dir2/f105", "") print result print len(list) if (result == 0): for i in list: print i.path EXAMPLE
#!/usr/bin/python import lfc """ # Using the lfc_getreplica method """ result, list = lfc.lfc_getreplica("/grid/dteam/antotests/extratests/dir2/f105", "", "") print result print len(list) if (result == 0): for i in list: print i.host print i.sfn EXAMPLE
#!/usr/bin/python import lfc """ # Using the lfc_getacl and lfc_setacl methods to add a user ACL """ nentries, acls_list = lfc.lfc_getacl("/grid/dteam/tests_sophie3", lfc.CA_MAXACLENTRIES) print nentries print len(acls_list) for i in acls_list: print i.a_type print i.a_id print i.a_perm # When adding a first ACL for a given user, you also need to add the mask # When adding the second user ACL, it is not necessary anymore acl_user = lfc.lfc_acl() acl_mask = lfc.lfc_acl() acl_user.a_type=2 # 2 corresponds to CNS_ACL_USER acl_user.a_id=18701 # user id acl_user.a_perm=5 acl_mask.a_type=5 # 5 corresponds to CNS_ACL_MASK acl_mask.a_id=0 # no user id specified acl_mask.a_perm=5 acls_list.append(acl_user) acls_list.append(acl_mask) res = lfc.lfc_setacl("/grid/dteam/tests_sophie3", acls_list) if res == 0: print "OK" else: err_num = lfc.cvar.serrno err_string = lfc.sstrerror(err_num) print "There was an error : Error " + str(err_num) + " (" + err_string + ")" sys.exit(1) EXAMPLE
#!/usr/bin/python import lfc """ # Using the lfc_getacl and lfc_setacl methods to remove a user ACL """ nentries, acls_list = lfc.lfc_getacl("/grid/dteam/tests_sophie3", lfc.CA_MAXACLENTRIES) # Note : you cannot remove the owner ACL (i.e. for CNS_ACL_USER_OBJ type) if ACLs # ====== for other users exist. Ff all the other user ACLs are deleted, the owner # ====== ACL is automatically removed. for i in acls_list: print i.a_type print i.a_id print i.a_perm del acls_list[1] # delete a given user ACL from the list of ACLs res = lfc.lfc_setacl("/grid/dteam/tests_sophie3", acls_list) if res == 0: print "OK" else: err_num = lfc.cvar.serrno err_string = lfc.sstrerror(err_num) print "There was an error : Error " + str(err_num) + " (" + err_string + ")" sys.exit(1) EXAMPLE
#!/usr/bin/env python import lfcthr import os from threading import Thread class slave(Thread): def __init__ (self): Thread.__init__(self) def run(self): .... result = lfcthr.lfc_getreplica("", guid, "") .... return if __name__ == '__main__': os.environ['LFC_HOST'] = 'my_lfc.cern.ch' # Threaded library initialisation lfcthr.init() .... # Start up of threads for i in xrange(totalNumberOfSlaves): slv = slave(i) slv.start() .... EXAMPLE
#!/usr/bin/python import lfc """ # Using the lfc_getusrmap method """ result, list = lfc.lfc_getusrmap() print result print len(list) if (result == 0): for i in list: print i.userid + " " + i.username EXAMPLE
#!/usr/bin/python import lfc """ # Using the lfc_getgrpmap method """ result, list = lfc.lfc_getgrpmap() print result print len(list) if (result == 0): for i in list: print i.gid + " " + i.groupname KNOWN BUGS
The current interface to the lfc_getcomment(3), lfc_getcwd(3), lfc_readlink(3), lfc_seterrbuf(3) requires the passing of str object which is modified to contain the result (in a similar way to the C functions, which accept a buffer). However this breaks the immutability of python str. This will be changed in the future. SEE ALSO
LFC C interface man pages LFC
$Date: 2010-02-04 13:08:39 +0100 (Thu, 04 Feb 2010) $ lfc_python(3)
All times are GMT -4. The time now is 06:27 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy