杀死 Bash 复制子进程来模拟崩溃

发布于 2024-10-01 07:57:43 字数 544 浏览 3 评论 0原文

我正在尝试测试一个 Bash 脚本,该脚本单独复制文件并对每个文件执行一些操作。它是可恢复的,所以我想确保正确测试它。什么是一个优雅的解决方案来终止或以其他方式中止从测试脚本进行复制的脚本,确保它没有时间复制和处理所有文件?

我有子进程的 PID,我可以更改两个脚本的源代码,并且我可以创建任意大的文件进行测试。

说明:我使用 & 在后台启动脚本,获取 PID 作为 $!,然后我有一个循环来检查该脚本中是否至少有一个文件目标目录(测试脚本复制三个文件)。此时我运行 kill -9 $PID,但该过程并未中断 - 文件已成功复制。即使文件足够大以至于创建它们(使用 dd/dev/urandom)需要几秒钟,也会发生这种情况。

是否只有当 cp 完成后,这些文件才对 shell 可见?这会有点奇怪,但它可以解释为什么 kill 命令为时已晚。

此外,这个想法不是测试恢复同一进程,而是切断第一个进程(模拟系统崩溃)并通过另一次调用恢复。

I'm trying to test a Bash script which copies files individually and does some stuff to each file. It is meant to be resumable, so I'd like to make sure to test this properly. What is an elegant solution to kill or otherwise abort the script which does the copying from the test script, making sure it does not have time to copy and process all the files?

I have the PID of the child process, I can change the source code of both scripts, and I can create arbitrarily large files to test on.

Clarification: I start the script in the background with &, get the PID as $!, then I have a loop which checks that there is at least one file in the target directory (the test script copies three files). At that point I run kill -9 $PID, but the process is not interrupted - The files are copied successfully. This happens even if the files are big enough that creating them (with dd and /dev/urandom) takes a couple seconds.

Could it be that the files are only visible to the shell when cp has finished? It would be a bit strange, but it would explain why the kill command is too late.

Also, the idea is not to test resuming the same process, but cutting off the first process (simulate a system crash) and resuming with another invocation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱要勇敢去追 2024-10-08 07:57:43

向子进程发送 KILL 信号:

kill -KILL $childpid

Send a KILL signal to the child process:

kill -KILL $childpid
不奢求什么 2024-10-08 07:57:43

您可以尝试使用大文件和睡眠来玩计时游戏。您可能对测试的重复性有疑问。

您可以将限制代码添加到测试脚本中,然后完全限制它。您可以通过传入一个值来限制代码,该值是:

  • 在循环中睡眠的睡眠值
  • 要处理的文件数
  • 脚本将在多少秒后结束
  • nice 执行脚本的值

从测试的角度来看,其中一些可能工作得更好或更差。 nice'ing 可能会给您带来不同的结果,就像设置一个后台进程在 N 秒后终止您的脚本一样。您还可以同时尝试其中一种以上,这可能会给您带来您想要的控制。例如,接受睡眠值和终止秒数可以为您提供细粒度的节流控制。

You can try an play the timing game by using large files and sleeps. You may have an issue with the repeatability of the test.

You can add throttling code to the script your testing and then just throttle it all the way down. You can do throttling code by passing in a value which is:

  • a sleep value for sleeping in the loop
  • the number of files to process
  • the number of seconds after which the script will die
  • a nice value to execute the script at

Some of these may work better or worse from a testing point of view. nice'ing may get you variable results, as will setting up a background process to kill your script after N seconds. You can also try more than one of these at the same time which may give you the control you want. For example, accepting both a sleep value and the kill seconds could give you fine grained throttling control.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文