从另一个脚本运行 bash 脚本并在第二个脚本运行时退出第一个脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27626800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run bash script from another script and exit first script while second is running
提问by Daniyal
First bash script is:
第一个 bash 脚本是:
./second #takes too long
I want kill first bash script while second bash script is still running.
我想在第二个 bash 脚本仍在运行时杀死第一个 bash 脚本。
Can I do that?
我可以这样做吗?
UPDATE
更新
First script run by cronjob!
由 cronjob 运行的第一个脚本!
回答by glenn Hymanman
If the last thing that the first script does is to call the second script, then do this:
如果第一个脚本所做的最后一件事是调用第二个脚本,请执行以下操作:
exec ./second
which replacesthe first script's process with the second.
它取代了与第二的第一个脚本的过程。
otherwise
除此以外
nohup ./second &
disown
回答by Brady
For Linux you would use the "kill" command after obtaining the PID of the program you wish to exit-
对于 Linux,您将在获得要退出的程序的 PID 后使用“kill”命令 -
http://linux.about.com/library/cmd/blcmdl_kill.htm
http://linux.about.com/library/cmd/blcmdl_kill.htm
I don't know how you would get the PID automatically via a script however.
但是,我不知道如何通过脚本自动获取 PID。
It appears you can also use "pkill" to terminate processes by name-
看来您还可以使用“pkill”按名称终止进程-
Ex-
前任-
pkill -9 ping
Or killall-
或者杀戮——
killall firefox
The answer below applies to BATCH files, not bash. I read the question wrong.
下面的答案适用于 BATCH 文件,而不是 bash。我读错了问题。
You can close an instance by getting the PID. You can also use this bit of code-
您可以通过获取 PID 来关闭实例。你也可以使用这段代码-
taskkill /f /im "x"
Where "x" is the name of the program you wish to close, as it would appear in the Task Manager.
其中“x”是您要关闭的程序的名称,它会出现在任务管理器中。
Explanation of the parameters I gave-
我给出的参数的解释-
/im ImageName : Specifies the image name of the process to be terminated. Use the wildcard (*) to specify all image names.
/im ImageName :指定要终止的进程的映像名称。使用通配符 (*) 指定所有图像名称。
/f : Specifies that process(es) be forcefully terminated. This parameter is ignored for remote processes; all remote processes are forcefully terminated.
/f :指定强制终止进程。远程进程忽略此参数;所有远程进程都被强制终止。
More info- http://technet.microsoft.com/en-us/library/bb491009.aspx
更多信息- http://technet.microsoft.com/en-us/library/bb491009.aspx