简体   繁体   English

Cron bash脚本-从未执行过查找命令

[英]Cron bash script - find command never execuced

Please look at my script to grab and parse xmltv data:请看我的脚本来抓取和解析 xmltv 数据:

#! /bin/bash

/bin/rm -f /home/hts/webgrab/multiple/out/*.xml

/home/hts/webgrab/multiple/1/run.sh &
/home/hts/webgrab/multiple/2/run.sh &
/home/hts/webgrab/multiple/3/run.sh &
/home/hts/webgrab/multiple/4/run.sh &
/home/hts/webgrab/multiple/5/run.sh &
/home/hts/webgrab/multiple/6/run.sh &
/home/hts/webgrab/multiple/7/run.sh &
/home/hts/webgrab/multiple/8/run.sh &
/home/hts/webgrab/multiple/9/run.sh &
/home/hts/webgrab/multiple/10/run.sh &
/home/hts/webgrab/multiple/11/run.sh &
/home/hts/webgrab/multiple/12/run.sh &
/home/hts/webgrab/multiple/13/run.sh &
/home/hts/webgrab/multiple/14/run.sh &
/home/hts/webgrab/multiple/15/run.sh &
/home/hts/webgrab/multiple/16/run.sh &
/home/hts/webgrab/multiple/17/run.sh &
/home/hts/webgrab/multiple/18/run.sh &
/home/hts/webgrab/multiple/19/run.sh &
/home/hts/webgrab/multiple/20/run.sh &
/home/hts/webgrab/multiple/21/run.sh &


wait

/usr/bin/tv_cat /home/hts/webgrab/multiple/out/*.* > /home/hts/webgrab/multiple/out/guide.xml

#copy result with timestamp
/bin/cp -rf /home/hts/webgrab/multiple/out/guide.xml "/var/www/guide/guide-$(date +"%Y%m%d").xml"

sleep 10

#find and rename file 5 day old
/usr/bin/find /var/www/guide -type f -name 'guide*' -mtime 5 -execdir /bin/mv {} /var/www/guide/pl.xml ';' 2> /home/hts/epg.log

As you can see, multiple webgrab program instances are executed, script waits for all of them to finish.如您所见,执行了多个webgrab程序实例,脚本等待它们全部完成。 Then, results are joined into single xmltv file (guide.xml), then guide.xml copied to /var/www/guide directory with timestamp.然后,将结果加入单个 xmltv 文件 (guide.xml),然后将 guide.xml 复制到带有时间戳的/var/www/guide目录。 Last step:最后一步:

/usr/bin/find /var/www/guide -type f -name 'guide*' -mtime 5 -execdir /bin/mv {} /var/www/guide/pl.xml ';' 2> /home/hts/epg.log

finds old file from 5 days ago -if exsists ( -mtime 5 ) and renames it (with overwrite old one) to pl.xml .查找 5 天前的旧文件 -if exsists ( -mtime 5 ) 并将其重命名(覆盖旧文件)为pl.xml

Problem: it looks like last command is never executed.问题:看起来最后一个命令从未执行过。 As a my cron job result, I'm getting only new timestamp file, but renamed pl.xml is always missing.作为我的 cron 作业结果,我只得到新的时间戳文件,但重命名的pl.xml总是丢失。 But if I run find command as above from command line, old file is renamed correctly to pl.xml但是,如果我从命令行运行上述find命令,旧文件将正确重命名为pl.xml

Any ideas what's wrong here?任何想法这里有什么问题? Full command paths are included to make sure they will run with cron.包含完整的命令路径以确保它们将与 cron 一起运行。 But it did not help, find and rename command does not work.但它没有帮助,查找和重命名命令不起作用。 Old file exists for sure, because it has been created 5 days earlier, but from some reason, rename command does not work from cron inside this script.旧文件肯定存在,因为它是在 5 天前创建的,但由于某种原因,重命名命令在此脚本中的 cron 中不起作用。

I know what's wrong, find command is the culprit - -mtime 5 options indeed returns files 5 days old.我知道出了什么问题, find命令是罪魁祸首 - -mtime 5选项确实返回了 5 天前的文件。 However, it also takes care to time, not only date.但是,它也需要注意时间,而不仅仅是日期。 So, as long as that's not full 5 days ago (including hour of day), find won't return anything.因此,只要 5 天前(包括一天中的某个小时)未满, find就不会返回任何内容。 My possible solutions:我可能的解决方案:

  • make sure I call find at spefic time to get always full 5 days确保我在特定时间打电话给find以便总是满 5 天
  • don't use mtime and parse timestamps from filenames instead (that seems to be simplest and most safe solution)不要使用mtime并从文件名解析时间戳(这似乎是最简单和最安全的解决方案)
  • find some way to force find command to ommit hours and use date only for its calculations (I actually have no idea how to do that)找到某种方法来强制find命令省略小时数并仅将日期用于其计算(我实际上不知道该怎么做)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM