当另一个程序使cronjob中的命令失败时,如何使用cronjob? [英] How can I use a cronjob when another program makes the commands in the cronjob fail?
问题描述
我有一个cron作业,将 cd
放入目录并执行操作.
I have a cron job which cd
into a directory and performs actions.
例如:
0 12,00 * * * cd /var/lib/test/0001 && cp *.zip /home/bobby/
但是,在/var/lib/test/0001
中创建.zip文件的程序每天都会更改目录名称.因此,第二天的目录为/var/lib/test/0002
,第三天的目录为/var/lib/test/0003
,依此类推.该模型无法更改.
However, the program that creates the .zip files in /var/lib/test/0001
changes the directory name every day. So on the second day, the directory is /var/lib/test/0002
and on the third day /var/lib/test/0003
and so on. This model cannot be changed.
当然,当目录从 0001
迁移到 0002
时,cronjob失败.
Of course, when the directory migrates from 0001
to 0002
, the cronjob fails.
是否可以使用cron将cd cd
转换为 000 *
,然后转换为 001 *
,依此类推,从而使 cp
命令会运行吗?也许有另一种方法?谢谢.
Is there a way to use cron to cd
into 000*
and then 001*
and so on so that the cp
command will be run? Perhaps there is an alternative way? Thank you.
3月13日修改:
还有一个我很难解决的问题.
There is another issue that I am finding hard to solve.
我只想对某个文件大小以上的文件进行 cp
个文件.我想将.zip文件复制到超过28,000个字节的/home/bobby/
中.如果它们少于28,000个字节,则不会被复制.谢谢,我该怎么办?
I only want to cp
files that are above a certain filesize. I want to copy .zip files to /home/bobby/
which are more than 28,000 bytes. If they are less than 28,000 bytes, then they don't get copied. How would I do this, thanks?
和以前一样,这会发生在/var/lib/test/****
中(其中 ****
来自 0000
为 FFFF
并每天递增).
As before, this would happen in /var/lib/test/****
(where ****
goes from 0000
to FFFF
and increments every day).
推荐答案
您可以使用示例脚本执行此操作:
You can do this with sample script:
dir=$(ls -tr1 /var/lib/test|tail -1)
cd /var/lib/test/$dir && cp *.zip /home/bobby/
ls
获取按时间反向排序的文件列表,因此最后一个是最后一个目录.然后我们进一步使用它.
ls
get the list of files sort by time in reverse order so the last one is the last directory. And then we use it further.
这篇关于当另一个程序使cronjob中的命令失败时,如何使用cronjob?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!