在Linux上,”rm”命令是否在后台运行?

huangapple go评论74阅读模式
英文:

On Linux, does the remove command "rm" run in the background?

问题

我正在尝试运行我的测试用例,大约有40,000个,使用以下脚本。

只显示部分脚本内容 -

#!/bin/bash
# 运行脚本

echo "请使用:nice nohup ./run_script 运行"

# 存储脚本的工作目录
WORKING_DIR=$(pwd)

# 用于构建和运行CMake CTest的临时目录
BUILD_DIR=${BUILD_DIR:-/localtemp/build}

# 清理并创建构建目录
rm -rf $BUILD_DIR
mkdir -p $BUILD_DIR
mkdir -p $BUILD_DIR/../result

cmake -G Ninja

ninja test

注意:我正在使用6个核心进行并行线程来运行我的测试用例。

在第一次尝试中,所有测试都通过了,这是正确的,因为我已经修复了所有测试用例中的错误。

但是,有时如果我想要重新运行相同的脚本,然后我在运行40,000个测试用例中的一些时会出现错误。但是,如果我单独运行那些失败的测试用例(一个接一个地运行),它们会完全通过。

因此,我假设rm -rf在删除旧的二进制文件和所有测试用例的符号(40 GB文件)时需要一些时间,所以我需要等待完全删除然后再次运行我的脚本。所以我应该在rm -rf命令之后添加一些延迟吗?

我在某处读到rm -rf将在完成工作后返回状态,然后才会执行下一个命令。但我的情况似乎显示rm -rf在后台运行。
这意味着我不应该立即启动新的运行,当我停止之前的运行时,我需要给一些时间来使用脚本中的rm -rf命令删除旧的输出(引入延迟),然后运行我的Ninja命令。这是真的吗?

英文:

I am trying to run my test cases which are nearly 40k with below scripts.

Just showing some part of script -

#!/bin/bash
# RUN script



echo "please run with: nice nohup ./run_script"

# working directory where script is stored
WORKING_DIR=$(pwd)

# temp directory to build and run the cmake ctest
BUILD_DIR=${BUILD_DIR:-/localtemp/build}

# clean and make build directory
rm -rf $BUILD_DIR
mkdir -p $BUILD_DIR
mkdir -p $BUILD_DIR/../result

cmake -G Ninja

ninja test

Note: I am using parallel threading with 6 cores to run my test cases .

In first attempt all are passing which is true as I fixed all the bugs in my test cases.

But some time If I want to re-run same script freshly, then I am getting the error in running some test cases out of 40k. But If I run that failing test cases separately(one by one) then they are passing perfectly.

So I assumed that rm -rf is taking some time to delete that old binary and symbols of all cases (40 GB files). so I need to wait for complete deletion and then run my script once again . So should I add some delay after rm -rf command in my script .

I read somewhere that rm -rf will return the status once it finishes the work .Then only next command executes. But my scnerio looks like showing that rm -rf is running in background.
Means I should not start new run immediately when I stopped the earlier run . I need to give some time to delete the old output from earlier run using rm -rf command in script (delay introduction) and run my below ninja command after that. Is that true?

答案1

得分: 1

编辑: 看起来,虽然一般性问题容易回答(即 rm 不会在后台运行),但您遇到的具体问题更为复杂,可能需要一些潜在的操作系统级调试。StackOverflow 上的任何人都无法为您执行此操作:D

针对您的特定问题,我建议使用与删除文件不同的策略,以避免可能的麻烦。为了确保您的构建目录是干净的,您可以在需要进行干净构建时使用一个新的临时目录。


初始答案:

rm 命令将不会以任何方式在后台或与脚本中的其他命令并发运行,除非您明确告诉它这样做(例如,在命令末尾使用 &)。一旦它完成(即脚本中的下一个命令执行),文件应该已被删除。因此,您的问题可能出现在其他地方。

尽管如此,如果您使用网络共享、FUSE 文件系统或任何其他改变操作系统对删除请求如何或何时响应的机制,可能会导致行为上的差异。

英文:

Edit: It appears that, while the general question is easy to answer (i.e. rm does not run in the background) the concrete problem you are having is more complex and requires some potentially OS-level debugging. Nobody on StackOverflow will be able to do this for you 在Linux上,”rm”命令是否在后台运行?

For your particular problem, I'd suggest using a different strategy than deleting the files to potentially avoid this hastle. In order to make sure that your build directory is pristine, you could use a new temporary directory in case you want to do a clean build.


Initial answer:

The rm command will not run "in the background" or in any way concurrent to other commands in your script unless you explicitly tell it to (e.g. using & at the end of the command). Once it has finished (i.e. the next command in your bash script executes) the files should have been deleted. Thus your problem probably lies somewhere else.

That being said, there may be differences in behaviour if, for example, you are using network shares, FUSE filesystems or any other mechanism that alters how or when your filesystem reacts to deletion request by the operating system.

答案2

得分: 1

这比较复杂一些。rm 不会在后台运行。可以确定的是,至少从逻辑上来说已经删除了(因此从用户的角度来看,东西已经被删除了);这意味着在某些操作系统上,即使命令已经终止,低级别的删除可能也在内核中完成(内核会在你不知情的情况下真正清理一些内部结构)。但这通常不会干扰你的脚本。你的问题肯定是在其他地方。

有可能一些进程仍然持有一些文件,即使它们看起来已经被删除了...因此,磁盘可能没有被释放,正如你期望的那样。然后,你将不得不等待这些进程完成,这样内核才会真正清理这些文件...

英文:

It's a little bit more complicated than that. rm will not run in background. What is sure is that the deletion is done at least logically (thus from user viewpoint things are deleted); this means that on some OSes it is possible that the low-level deletion is done in the kernel even if the command has terminated (the kernel then really clean some internal structures behind your back). But that should not normally interfer with your script. Your problem is surely elsewhere.

It could be that some processes hold some files even if they seems deleted... Thus the disk could be not freed as you expected. You'll then have to waut completion of those processes so that kernel really cleans the files...

huangapple
  • 本文由 发表于 2020年1月6日 19:27:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611295.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定