[英]Linux shell scripting: How can I stop a first program when the second will have finished?
I have two programs in Linux (shell scripts, for example): 我在Linux中有两个程序(例如,shell脚本):
NeverEnding.sh
AllwaysEnds.sh
The first one does never stop , so I wanna run it in background . 第一个永远不会停止 ,所以我想在后台运行它。
The second one does stop with no problem. 第二个确实没有问题。
I would like to make a Linux shell script that calls them both , but automatically stops (kill, for example) the first when the second will have finished . 我想制作一个可以同时调用它们的Linux shell脚本 ,但是当第二个 脚本结束时 , 它们会自动停止(例如杀死)。
Specific command-line tools allowed, if needed. 如果需要,允许使用特定的命令行工具。
You can send the first into the background with &
and get the PID of it by $!
您可以使用
&
将第一个发送到后台,并通过$!
获得它的PID $!
. 。 Then after the second finishes in the foreground you can kill the first:
然后,在前景中的第二个完成之后,您可以杀死第一个:
#!/bin/bash
NeverEnding.sh &
pid=$!
AllwaysEnds.sh
kill $pid
You don't actually need to save the pid in a variable, since $!
由于
$!
,您实际上不需要将pid保存在变量中$!
only gets updated when you start a background process, it's just make it more easy to read. 仅在启动后台进程时才会更新,这只是使其更易于阅读。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.