简体   繁体   中英

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. That means that the sleep 5 command never gets to run.

You need to put the Firefox process into the background like this:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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