Linux Perl:如何查找文件的存在并读取它(如果存在)>即时的

时间:2015-08-21 13:08:43

标签: linux perl sleep daemon

有类似的问题,但我所追求的是实时检测文件的存在。

我有2个linux box / PC。

[Linux Box 1] - >生成名为主机(手动执行)

的文件

[Linux Box 2] - >有一个守护进程,每2秒休眠一次,检查预期的文件是否存在。如果它存在,它将执行我的ssh命令(当然会显示一条文本显示它已被执行bla bla bla)。

PERL SCRIPT:

ctr:
if (-e $file) { system ("ssh $script $host.$display"); } 
sleep(2);
goto ctr;

问题 问题是,似乎我无法确定该文件是否实时存在,即使它已经存在。

BOX 1

[machine01:/home/machine01]% /MACHINE/TOOLS/perl/app/host_gen
> executed at 08/21/2015 21:40:32

BOX 2

Sleeping for 2 seconds [08/21/2015 21:40:22]
Sleeping for 2 seconds [08/21/2015 21:40:24]
Sleeping for 2 seconds [08/21/2015 21:40:26]
Sleeping for 2 seconds [08/21/2015 21:40:28]
Sleeping for 2 seconds [08/21/2015 21:40:30]
Sleeping for 2 seconds [08/21/2015 21:40:32] **<-expected execution**
Sleeping for 2 seconds [08/21/2015 21:40:34]
Sleeping for 2 seconds [08/21/2015 21:40:36]
Sleeping for 2 seconds [08/21/2015 21:40:38]
Sleeping for 2 seconds [08/21/2015 21:40:40]
Sleeping for 2 seconds [08/21/2015 21:40:42]
Sleeping for 2 seconds [08/21/2015 21:40:44]
Sleeping for 2 seconds [08/21/2015 21:40:46]
Sleeping for 2 seconds [08/21/2015 21:40:48]
Sleeping for 2 seconds [08/21/2015 21:40:50]
Sleeping for 2 seconds [08/21/2015 21:40:52]
Sleeping for 2 seconds [08/21/2015 21:40:54]
Sleeping for 2 seconds [08/21/2015 21:40:56]
Sleeping for 2 seconds [08/21/2015 21:40:58]
Sleeping for 2 seconds [08/21/2015 21:41:00]
Sleeping for 2 seconds [08/21/2015 21:41:02]
Sleeping for 2 seconds [08/21/2015 21:41:04]
Sleeping for 2 seconds [08/21/2015 21:41:06]
Sleeping for 2 seconds [08/21/2015 21:41:08]
Sleeping for 2 seconds [08/21/2015 21:41:10]
Sleeping for 2 seconds [08/21/2015 21:41:12] <- It was executed here
TYPE_AND_DISPLAY [wkss machine01.ult.ultera.omps.com:0]
TYPE [wkss] DISPLAY [machine01.ult.ultera.omps.com:0]

检测现有文件几乎需要一分钟或有时10秒钟。

这很奇怪。

你们有任何想法我怎么能实现我的目标?

提前致谢。

1 个答案:

答案 0 :(得分:8)

Prior serverfault.com question: How can I tell if a file is on a remote filesystem with Perl?

引用该线程;

  

默认情况下,大多数NFS客户端将目录信息缓存60秒。

该线程中提到的解决方案:

  

使用noac选项在客户端上挂载NFS共享以禁用属性缓存,或者使用acdirmin = 0,acdirmax = 0来禁用目录缓存。