Bash 脚本启动 Chromium,休眠 20 秒,然后安静关闭
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/11834065/
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
Bash script to launch chromium, sleep for 20 seconds, then close quietly
提问by sifuhall
I am trying to create a bash script that will launch chromium, wait 20 seconds, then close chromium.
我正在尝试创建一个 bash 脚本,该脚本将启动铬,等待 20 秒,然后关闭铬。
This is for xbmcbuntu so I can open a site, then it will close automatically after 20 seconds (as I will have no way of closing it with just the remote control).
这是针对 xbmcbuntu 的,所以我可以打开一个站点,然后它会在 20 秒后自动关闭(因为我无法仅用遥控器关闭它)。
What I have is:
我所拥有的是:
#!/bin/bash
openbox &
/usr/bin/chromium-browser
sleep 20
killall -9 openbox
Chromium opens ok, but never closes.
Chromium 可以正常打开,但永远不会关闭。
What am i missing?
我错过了什么?
采纳答案by jordanm
Since you are not putting chromium-browser in the background, none of the code after the chromium-browser command will execute until chromium-browser finishes execution. This should do what you want:
由于您没有将chromium-browser 置于后台,因此在chromium-browser 完成执行之前,chromium-browser 命令之后的任何代码都不会执行。这应该做你想做的:
#!/bin/bash
openbox &
openbox_pid=$!
/usr/bin/chromium-browser &
chrome_pid=$!
sleep 20
kill "$chrome_pid" "$openbox_pid"
回答by chepner
Instead of killing openbox, you need to kill chromium-browser.
与其杀人openbox,不如杀人chromium-browser。
openbox &
openbox_pid=$!
/usr/bin/chromium-browser &
chromium_pid=$!
sleep 20
kill $chromium_pid
kill $openbox_pid
Don't use kill -9unless it's absolutely necessary.
kill -9除非绝对必要,否则不要使用。
回答by Vakho
Not tested, only my idea
没有测试,只是我的想法
#!/bin/bash
openbox &
#!/bin/bash openbox &
here run: script.any &    #background where script.any permission is execute and contain:
#!/bin/bash
这里运行:script.any & #background 其中 script.any 权限被执行并包含:
 #!/bin/bash
/usr/bin/chromium-browser
/usr/bin/chromium-browser
from this script run browser show mode
从此脚本运行浏览器显示模式
sleep 20 killall -9 openbox
睡眠 20 killall -9 openbox
sorry my bad english Anjoy ;)
对不起,我的英语不好 Anjoy ;)

