运行量子化学计划时我必须处理一些问题。该程序在运行时似乎会创建两个重要文件,但在使用它们后会再次直接删除它们。这些文件对于获得支持非常重要。但它们被删除得太快,我无法手工复制它们。
由于似乎没有办法告诉程序应该保存这些文件我正在考虑一个小脚本,它不断扫描工作目录中的那些文件。创建它们时,我希望该程序将它们复制到另一个目录或仅复制到另一个文件名。
尽管我提供了这个问题的标签,但是没有特殊权利的任何编程语言都可以访问" CentOS版本6.3(最终)" - 正常用户是正确的。
我正在考虑通过包含find -name ...
的while循环执行此操作,并在发现/复制文件时进行回显。但这种方法对我来说似乎很愚蠢,我希望对更好的解决方案提出一些意见。
我要保存的文件是filename.gcp.in.tmp
和filename.gcp.out.tmp
。
答案 0 :(得分:3)
A useful trick for grabbing temporary files is to create hard links to them. That works better than copying because it doesn't matter how big the temporary files are, and there is no danger of missing some of the contents by copying before the file is complete. When the program that created a temporary file deletes it, the hard link gets left behind.
If you don't know the names of the temporary files in advance, and you can't use inotify
, one possible way to do it is to run this polling loop in the directory where the files are created:
while : ; do
for tmpfile in *.gcp.in.tmp *.gcp.out.tmp ; do
[[ -e $tmpfile ]] || continue
backupfile=$tmpfile.bak
[[ -e $backupfile ]] || ln -- "$tmpfile" "$backupfile"
done
done
If you know the filenames in advance, and the program that creates them just opens them for writing, closes them, and deletes them, you can do without the loop. Do this before running your program:
touch filename.gcp.in.tmp filename.gcp.out.tmp
ln filename.gcp.in.tmp filename.gcp.in.tmp.bak
ln filename.gcp.out.tmp filename.gcp.out.tmp.bak
The contents of the temporary files should be in the .bak
files when the program has finished running.
答案 1 :(得分:2)
您可以使用inotify和it seems to be built into CentOS 6.3。
例如(摘自a tutorial where you'll find more details):
while true #run indefinitely
do
inotifywait -r -e modify,attrib,close_write,move,create,delete /dir && /bin/bash backup-script
done
backup-script
是您用来验证所需文件的创建并将其复制到别处的任何内容。您也可以使用incron。