[英]How to keep checking a directory in linux system if a log file is created or no? Using python
A feature will be launched which will create logs as the run proceeds.将启动一项功能,该功能将在运行过程中创建日志。 So, I have to write a script which will keep on checking a directory during runtime of the about feature if there are any log files created or no and incase if I see the logs being created, I will do further actions.因此,我必须编写一个脚本,该脚本将在关于功能的运行时继续检查目录是否创建了任何日志文件,如果我看到正在创建的日志,我将执行进一步的操作。 The tricky part here is I do not have access to watch, cron jobs or anything like that at customer site so any other suggestion would be appreciated.这里棘手的部分是我无法在客户站点访问 watch、cron 作业或类似的东西,因此任何其他建议将不胜感激。 Also, I cannot install any python libraries, so I need something very basic.另外,我无法安装任何 python 库,所以我需要一些非常基本的东西。
I haven't yet tried but looking to see if any function exists, I am planning to use while loop to keep monitoring the directory.我还没有尝试过,但想看看是否存在任何 function,我打算使用 while 循环来继续监视目录。
If you cannot convince the client to install inotify-tools in order to have access to inotifywait , then you need to keep track of not only the existence of an output file, but, more important, is the output file closed (is the process finished with that file).如果您无法说服客户端安装inotify-tools以访问inotifywait ,那么您不仅需要跟踪 output 文件的存在,而且更重要的是,output 文件是否已关闭(进程是否已完成与该文件)。
In other words the process creating the file would have a second " flag file " (call it ${process_name}.writing ) which would be created before output and removed when output completed.换句话说,创建文件的进程将有第二个“标志文件”(称为${process_name}.writing ),它将在 output 之前创建并在 output 完成时删除。
As for conditional logic, if output.txt exists and ${process_name}.writing does not, then the output.txt is complete and usable .至于条件逻辑,如果output.txt存在而${process_name}.writing不存在,那么output.txt是完整可用的。
You can always consider use of the flock utility to test/assign reserved use of file in order to ensure no conflicts in acquisition of "closed" files.您始终可以考虑使用flock实用程序来测试/分配文件的保留使用,以确保在获取“已关闭”文件时不会发生冲突。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.