Linux 下监测指定路径下指定时间间隔内是否有指定的文件的生成


题目很拗口,感觉自己有必要说明一下,O(∩_∩)O~

在 Liunx 程序设计中,有时我们需要写这样一个程序,当指定目录下有相应的新文件生成时,触发程序动作,这个触发的动作可能是解析新生成的文件异或其他行为。

一种实现方法是在主程序中运行一个循环监测程序,监测指定目录下指定时间间隔内有没有指定的新文件生成,如果有则触发相应的解析动作等行为。

下面是自己写的一个脚本文件,功能就是做这样一件事情:

  1. #!/bin/bash  
  2.   
  3. #program:   
  4. #   后台脚本,查找 path 路径下最近 n 秒内有没有新文件(文件名 filename )生成   
  5. #   path        脚本输入参数,查找路径   
  6. #   n           脚本输入参数,查找时间间隔(s)   
  7. #   filename    脚本输入参数,查找文件名   
  8. #   如果有生成,返回0;反之没有生成,返回1;脚本没有正常工作结束,返回2   
  9.   
  10. #History:   
  11. #   2011/06/11  wwang   first release   
  12.   
  13. # 获得当前世纪秒   
  14. nowSoc=$(date +%s)  
  15. # 输出   
  16. #echo -e "Now time is $nowSoc..."   
  17.   
  18. # 判断脚本输入参数是否合法   
  19. if test $# -eq 3; then  
  20.   
  21.     # 设置查找路径   
  22.     path=$1  
  23.       
  24.     # 设置查找时间间隔   
  25.     delta=$2  
  26.     # 自减delta秒   
  27.     nowSoc=$(($nowSoc - $delta))  
  28.     # 输出   
  29.     #echo -e "$nowSoc"   
  30.       
  31.     # 设置查找文件名   
  32.     filename=$3  
  33.   
  34.     # 查找控制输出文件   
  35.     theControlFile=$(find $path -name $filename)  
  36.     if [ -f "$theControlFile" ]; then  
  37.         # 输出   
  38.         #echo -e "The new Control file is $theControlFile..."   
  39.       
  40.         # 获得文件的修改时间   
  41.         theFileChangeSoc=$(stat -c "%Z" $theControlFile)  
  42.         # 输出   
  43.         #echo -e "The file change time is $theFileChangeSoc..."   
  44.           
  45.         # 判断最近 n 秒内是否生成了对应文件   
  46.         if test $(($theFileChangeSoc - $nowSoc)) -ge 0; then  
  47.             # 输出   
  48.             #echo -e "Find"   
  49.           
  50.             # 返回 0   
  51.             exit 0  
  52.         else  
  53.             # 输出   
  54.             #echo -e "Not Find"   
  55.           
  56.             # 返回 1   
  57.             exit 1  
  58.         fi  
  59.       
  60.     else  
  61.         # 输出   
  62.         #echo -e "This is no new Control file..."   
  63.       
  64.         # 返回 1   
  65.         exit 1  
  66.     fi  
  67.       
  68. else  
  69.     # 输出   
  70.     echo -e "This is no three parameters..."  
  71.       
  72.     # 返回 2   
  73.     exit 2  
  74. fi

下面是自己的调用示例,采用了 System 函数进行脚本调用,System 函数的相应知识可以参阅这篇文章  。

  1. #include <stdio.h>  
  2. #include <stdlib.h>   
  3. #include <sys/wait.h>   
  4. // 可不需要包含   
  5. //#include <sys/types.h>   
  6.   
  7. int main()  
  8. {  
  9.     pid_t status;  
  10.     /*  功能: 调用后台脚本,查找 path 路径下最近 n 秒内有没有新文件 
  11.                 (文件名 filename )生成 
  12.         调用事例:脚本路径/findTheNewFile.sh path n filename 
  13.                 path        脚本输入参数,查找路径 
  14.                 n           脚本输入参数,查找时间间隔(s) 
  15.                 filename    脚本输入参数,查找文件名 
  16.         脚本返回:如果有生成,返回0; 
  17.                 反之没有生成,返回1; 
  18.                 脚本没有正常工作结束,返回2  
  19.     */  
  20.     status = system("./findTheNewFile.sh . 2 voltgecontroloutput.txt");  
  21.     /* 脚本调用失败 */  
  22.     if (-1 == status)      
  23.     {      
  24.         printf("system error!\n");      
  25.     }      
  26.     else     
  27.     {      
  28.         //printf("exit status value = [0x%x]\n", status);      
  29.            
  30.         // 判断脚本是否执行成功   
  31.         if (WIFEXITED(status))      
  32.         {      
  33.             // 脚本正确返回   
  34.             if (0 == WEXITSTATUS(status))      
  35.             {      
  36.                 printf("run shell script successfully, and find the new Control file.\n");      
  37.             }     
  38.             else if (1 == WEXITSTATUS(status))  
  39.             {  
  40.                 printf("run shell script successfully, but not find the new Control file.\n");      
  41.             }   
  42.             else /*if (2 == WEXITSTATUS(status))*/     
  43.             {      
  44.                 printf("run shell script fail, script exit code: %d\n", WEXITSTATUS(status));      
  45.             }      
  46.         }      
  47.         else      
  48.         {      
  49.             printf("exit status = [%d]\n", WEXITSTATUS(status));      
  50.         }      
  51.     }    
  52.       
  53.     return 0;  
  54. }

相关内容

    暂无相关文章