bash - 如何在前一个命令之前执行后一个命令?
我想编写一个脚本来模拟由于某种原因而中断的进程。
所以我尝试运行一些杀死进程的东西 像这样的事情:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不需要使用
ssh
在后台运行某些内容。使用这个:注意“&”它将整个子 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:Notice the "&" which puts the whole subshell in the background. Inside the subshell, first a
sleep
is run, then akill
.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.尝试引用您的 ssh 命令,如下所示。如果不加引号,则分号标记 ssh 命令的结束,因此第二个
kill
命令将在 ssh 命令放入后台后立即执行。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 secondkill
commmand is executed immediately after the ssh command has been put in the background.使用
&
在后台运行您的进程。完毕。
Use
&
to run your process in the background.Done.