所以我有两个文件,如果这两个文件中的任何一个都出现一个新行,我想接收一个包含该行内容的IM(最好是jabber或gTalk)。 你们对Linux守护进程有什么build议吗?
如果您正在通过sysloglogin, Metalog支持在符合某些条件的消息被logging时执行命令。 否则,您可以使用tailf
来查看日志文件中的新行。
sendxmpp是一个小的Perl脚本来发送XMPP消息(可能已经可以作为你喜欢的分发包)
你可以用一个shell脚本将这两个文件缝合在一起,没有太多的困难。 对于metalog的情况下,创build一个这样的脚本:
#!/bin/sh echo $* |sendxmpp [email protected]
并添加command = /path/to/script.sh
到metalog.conf的相关部分
对于尾巴的情况,你可以尝试这样的事情,以持久的方式运行:
tailf /var/log/file-to-watch.log |(while true; do read M; echo $M | sendxmpp [email protected]; done)
sendxmpp需要一个有效的XMPP帐户,请参阅手册页以了解如何configuration要使用的帐户。
(根据我的经验,如果XMPP提供的错误消息过于频繁,往往会变得非常烦人……)
我做了这个小python脚本。 你可以用它作为起点
import xmpp, os, time login = 'Your.Login' # @gmail.com pwd = 'YourPassword' recipient = '[email protected]' logfile = "/home/myself/test.log" def sendmsg(text): global login, pwd, recipient cnx = xmpp.Client('gmail.com') cnx.connect( server=('talk.google.com',5223) ) cnx.auth(login,pwd, 'botty') cnx.send( xmpp.Message( recipient , text ) ) oldsize = newsize = os.path.getsize(logfile) while True: newsize = os.path.getsize(logfile) if newsize != oldsize: f = open(logfile) f.seek(oldsize, os.SEEK_SET) s = f.read() if s[-1] == '\n': sendmsg(s) oldsize = f.tell() f.close() time.sleep(10)
我使用该页面上的信息将xmpppy连接到Google Talk。