bash - 如何在前一个命令之前执行后一个命令?

发布于 2024-12-26 05:26:52 字数 409 浏览 0 评论 0原文

我想编写一个脚本来模拟由于某种原因而中断的进程。

所以我尝试运行一些杀死进程的东西 像这样的事情:

ssh -f localhost sleep 7;kill pid
call_function_from_another_script_that_runs_the_process

希望 ssh 命令由于“-f”而在后台运行,并且由于睡眠而不会很快执行kill命令。 问题是 sleep 在这里不起作用。杀戮正在立即执行。

如果我在没有 -f 的情况下运行 ssh,那么第二行不会被调用,我的进程也不会运行。

请假设第二行正如它所说 - 从另一个脚本运行一个函数来运行进程。我无法“将该函数放入脚本中并运行它”或更改其他已编写的内容。

有什么想法吗?

谢谢。

I want to write a script that simulates a proccess being interrupted for some reason.

So I try to run something that kills the process
something like this:

ssh -f localhost sleep 7;kill pid
call_function_from_another_script_that_runs_the_process

hoping that the ssh command will run in the background because of "-f", and the kill commands will not be executed soon enough because of the sleep.
The problem is that sleep doesn't take effect here. the kill is being executed right away.

if I run the ssh without -f, so the second line isn't called and my process doesn't run.

please assume that the second line is as it says - running a function from another script the runs the process. I cant "put that function in a script and run it" or something that changes other things that are already written.

Any idea?

thanks.

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

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

发布评论

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

评论(3

那些过往 2025-01-02 05:26:52

您不需要使用 ssh 在后台运行某些内容。使用这个:

(
    sleep 7
    kill pid
) &
call_function_from_another_script_that_runs_the_process

注意“&”它将整个子 shell 置于后台。在子 shell 内,首先运行 sleep,然后运行 ​​kill

顺便说一句,您示例中的 kill 命令之所以立即生效,是因为该脚本行有两个命令:“ssh -f localhost sleep 7”和“ kill pid”,而不是 SSH 会话中的一个命令(“sleep 7;kill pid”)。

You don't need to use ssh to run something in the background. Use this:

(
    sleep 7
    kill pid
) &
call_function_from_another_script_that_runs_the_process

Notice the "&" which puts the whole subshell in the background. Inside the subshell, first a sleep is run, then a kill.

Incidentally, the reason why the kill command in your example takes effect right away is because that line of the script has two commands: "ssh -f localhost sleep 7" and "kill pid", not one command ("sleep 7;kill pid") inside an SSH session.

偏爱自由 2025-01-02 05:26:52

尝试引用您的 ssh 命令,如下所示。如果不加引号,则分号标记 ssh 命令的结束,因此第二个 kill 命令将在 ssh 命令放入后台后立即执行。

ssh -f localhost "sleep 7;kill pid"
call_function_from_another_script_that_runs_the_process

Try quoting your ssh command as shown below. If you don't quote, the semicolon marks the end of the ssh command and so the second kill commmand is executed immediately after the ssh command has been put in the background.

ssh -f localhost "sleep 7;kill pid"
call_function_from_another_script_that_runs_the_process
往日情怀 2025-01-02 05:26:52

使用 & 在后台运行您的进程。

some_long_running_process &
pid=$!
sleep 7
kill ${pid}

完毕。

Use & to run your process in the background.

some_long_running_process &
pid=$!
sleep 7
kill ${pid}

Done.

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