后台进程的返回值 [英] Return value of background process
问题描述
我需要的基础上,即如果它摆在首位终止后台进程的返回值来采取一些行动。
I need to take some action based on the return value of a background process ie if it terminates in the first place.
具体做法是:在理想的操作,这是我作为运行后台进程的服务器仅会继续运行下去。在这种情况下保持它的背景是有道理的,因为我希望我的shell脚本来产卵服务器之后做其他事情。但是,如果服务器异常终止,我想preferably使用exit返回值从服务器来决定是否要杀死我的主脚本或没有。如果这是不可能的我至少要中止主脚本,而不是与发生故障的服务器上运行。
Specifically : in ideal operation, the server which I run as the background process will just keep running forever. In such cases keeping it in the background makes sense, since I want my shell script to do other things after spawning the server. But if the server terminates abnormally, I want to preferably use the exit return value from the server to decide whether to kill my main script or not. If that's not possible I at least want to abort the main script rather than run it with a failed server.
我要寻找在shell脚本异步回调的本质的东西。一种解决方案是产生一个监控进程,如果服务器出现故障或不是定期检查。 preferably我希望主shell脚本本身做没有了。
I am looking for something in the nature of an asynchronous callback for shell scripts. One solution is to spawn a monitoring process that periodically checks if the server has failed or not. Preferably I would want to do it without that within the main shell script itself.
推荐答案
您可以窝在脚本里的后台进程。例如,如果要发送到后台的过程称为富:
You could nest the background process inside a script. For example, if the process you wish to send to the background is called foo:
#!/bin/sh
foo
if [ $? ]
then
# do something here if process failed
fi
然后,只需在后台,而不是富
运行上面的脚本。你可以杀死它,如果你需要关闭它,否则它永远不会只要终止为富
继续运行,如果富死了,你可以做任何你想要的根据其错误code。
Then just run the above script in the background instead of foo
. you can kill it if you need to shut it down, but otherwise it will never terminate as long as foo
continues to run, and if foo dies, you can do whatever you want to based on its error code.
这篇关于后台进程的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!