Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 在后台进程中写入文件_Bash_Background Process_Pid_Fswatch - Fatal编程技术网

Bash 在后台进程中写入文件

Bash 在后台进程中写入文件,bash,background-process,pid,fswatch,Bash,Background Process,Pid,Fswatch,我正在尝试创建一个脚本,该脚本将处理目录中的修改/新文件(通过lftp从远程目录镜像,但这是另一个故事) 要跟踪修改的文件,我使用fswatch。然后,我将fswatch检测到的文件从xml转换为json,并将它们存储在单独的目录中。为了确保在没有更多文件要处理时(镜像作业结束时)可以停止此转换,我会跟踪镜像过程完成后将创建的文件 我的脚本可以工作,但由于一个奇怪的原因,在镜像作业完成之前,我看不到json文件。这就好像转换后的文件存储在内存中的某个地方,一旦“停止”条件为真,这些文件就会神奇地

我正在尝试创建一个脚本,该脚本将处理目录中的修改/新文件(通过
lftp
从远程目录镜像,但这是另一个故事)

要跟踪修改的文件,我使用
fswatch
。然后,我将
fswatch
检测到的文件从xml转换为json,并将它们存储在单独的目录中。为了确保在没有更多文件要处理时(镜像作业结束时)可以停止此转换,我会跟踪镜像过程完成后将创建的文件

我的脚本可以工作,但由于一个奇怪的原因,在镜像作业完成之前,我看不到json文件。这就好像转换后的文件存储在内存中的某个地方,一旦“停止”条件为真,这些文件就会神奇地出现在目录中

这是正常的行为吗?如何使文件在处理后立即显示?我可以通过什么方式优化我想要实现的目标?(我是bash方面的新手……一般来说,我是编程方面的新手。)

以下是我使用的脚本:

my_convert_xml_to_json_function () {
    if [ -f "$1" ]; then
        temporary_file_name_for_json=$(echo "${1/$path_to_xml_files\/}" | base64)
        xml2json < "$1" | jq -rc '.amf' > "${path_to_json_files}/${temporary_file_name_for_json}.txt"
    fi
}
export -f my_convert_xml_to_json_function
export path_to_xml_files
export path_to_json_files

# repeat watching for files until the mirroring is over
fswatch -0 --event Updated --event Created "${path_to_xml_files}" | grep -ai 'xml$' | xargs -0 -n 1 -I {} bash -c 'my_convert_xml_to_json_function "{}"' & 

temporary_pid_of_fswatch=`jobs -p`
echo "This is PID of the last bit in the pipeline: $!; this is PID of the fswatch: ${temporary_pid_of_fswatch}"


# now check for the existence of a stopping rule
while [[ $(shopt -s nullglob; set -- "${my_temporary_files}"/xml-mirrorring-started-on-*-is-completed.txt; echo $#) -eq 0 ]]; do
    # tell the script to stop and remove the file generated by the mirror into the trashcan
        sleep 1 && temp_continue_check="running `date`"
        echo "Stop condition met (${temp_continue_check})."
done && kill -15 "${temporary_pid_of_fswatch}" && mv -v "${my_temporary_files}"/xml-mirrorring-started-on-*-is-completed.txt "$my_trashcan"
my_convert_xml_to_json_函数(){
如果[-f“$1”];则
临时_文件_名称_for_json=$(echo“${1/$path_to_xml_文件\/}”| base64)
xml2json<“$1”| jq-rc.amf'>”${path_to_json_files}/${temporary_file_name_for_json}.txt”
fi
}
export-f my_convert_xml_to_json_函数
将路径\u导出到\u xml\u文件
将路径\导出到\ json\文件
#重复监视文件,直到镜像结束
fswatch-0——事件更新——事件创建了“${path_to_xml_files}”| grep-ai'xml$'| xargs-0-n1-I{}bash-c'my_convert_xml_to_json_函数“{}”&
_fswatch的临时_pid_=`jobs-p`
echo“这是管道中最后一位的PID:$!;这是fswatch的PID:${temporary_PID_of_fswatch}”
#现在检查是否存在停止规则
而[[$(shopt-s nullglob;set--“${my_temporary_files}”/xml镜像开始于-*-is-completed.txt;echo$)-eq 0];做
#告诉脚本停止并将镜像生成的文件删除到垃圾桶中
睡眠1&temp\u continue\u check=“running`date`”
echo“满足停止条件(${temp\u continue\u check})”
done&&kill-15“${temporary\u pid\u of_fswatch}”和&mv-v“${my\u temporary\u files}”/xml镜像从-*-is-completed.txt“$my\u trashcan”开始

编辑:所以在@snort的评论之后,如果我将
sync
添加到脚本中,那么我就能够获得文件的“实时”更新。否则,文件就在空中某处。。。如果一个进程在后台运行,我键入
sync
,我会得到一个新的进程,它似乎“冻结”(基于
top
输出,我可以看到它正在做什么,但我没有看到处理过的文件像它们应该(最终)那样写入文件夹)。有没有办法强制OSX将这些文件写入磁盘(脚本中不包括同步)?

如果文件很小,您可能会认为它们保存在内存中。在每个文件之后使用flush命令可能会达到您的目的。如果你只是想看看正在处理的文件,你也可以抛出一个${path_to_json…}/${temporary…},它将首先创建文件,这样你就可以在文件完全写入之前看到它。文件非常小,平均每个文件不到3KB。嗯。。。那么,如果内存中的文件超过内存所能处理的数量,我是否可能丢失信息?除非您的RAM坏了:)。顺便说一句,这是Linux的默认行为已经有一段时间了。我还说了flush命令,但该命令是sync,它刷新内存并使其写入磁盘。@snort的手册页:谢谢,我在Mac电脑上,所以希望他们的RAM既好又贵。我想你使用xargs会使文件名发出咕噜声,直到整个过程停止,然后xargs启动创建文件的脚本。