简体   繁体   English

为什么我不能杀死 bash 中的进程?

[英]Why can't I kill a process in bash?

I have the following script:我有以下脚本:

firefox <url>
sleep 5
kill -9 $(pidof firefox)

Thanks in advance提前致谢

When I run firefox like that on my Linux (Ubuntu 18.04) system the firefox command runs as a foreground process.当我在我的 Linux(Ubuntu 18.04)系统上运行firefox时,firefox 命令作为前台进程运行。 That means that the sleep 5 command never gets to run.这意味着sleep 5命令永远不会运行。

You need to put the Firefox process into the background like this:您需要将 Firefox 进程放入后台,如下所示:

firefox <url> &
sleep 5
kill -9 $(pidof firefox)

It works for me.这个对我有用。

You can save the pid in a variable, for a pure shell solution.您可以将 pid 保存在变量中,以获得纯 shell 解决方案。

firefox <url> & pid=$!
sleep 5
kill "$pid"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM