自动化Killall然后Killall 9级 [英] Automating Killall then Killall level 9

查看:200
本文介绍了自动化Killall然后Killall 9级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时我想 killall 有一定的过程,但运行 killall 不起作用。所以,当我尝试再次启动的过程中,它失败,因为在previous会话仍在运行。然后我必须在它运行的繁琐 killall -9 。因此,为了简化我的生活,我创建了一个 realkill 脚本,它是这样的:

Sometimes I want to killall of a certain process, but running killall doesn't work. So when I try to start the process again, it fails because the previous session is still running. Then I have to tediously run killall -9 on it. So to simplify my life, I created a realkill script and it goes like this:

PIDS=$(ps aux | grep -i "$@" | awk '{ print $2 }') # Get matching pid's.
kill $PIDS 2> /dev/null # Try to kill all pid's.
sleep 3
kill -9 $PIDS 2> /dev/null # Force quit any remaining pid's.

所以,这是在做这个的最好方法?有什么办法,我可以改善这个脚本?

推荐答案

避免 killall 如果你可以因为没有在所有UNIX平台上一致地实施。 Proctools' pkill的指派,是preferable:

Avoid killall if you can since there is not a consistent implementation across all UNIX platforms. Proctools' pkill and pgrep are preferable:

for procname; do
    pkill "$procname"
done

sleep 3
for procname; do
    # Why check if the process exists if you're just going to `SIGKILL` it?
    pkill -9 "$procname"
done

(编辑)如果你有,都应该被杀死后重新启动过程中,你可能不希望盲目地杀死他们,因此你可以首先收集的PID:

(Edit) If you have processes that are supposed to restart after being killed, you may not want to blindly kill them, so you can gather the PIDs first:

pids=()
for procname; do
    pids+=($(pgrep "$procname"))
done
# then proceed with `kill`

这是说,你应该尽量避免使用 SIGKILL 如果你能。它不给软件一个机会后自己清理。如果在接收后不久,程序不会退出一个 SIGTERM 它可能在等待着什么。找出它的等待(硬件中断?打开文件?),并解决这个问题,你可以让它关闭干净。

That said, you should really try to avoid using SIGKILL if you can. It does not give software a chance to clean up after itself. If a program won't quit shortly after receiving a SIGTERM it is probably waiting for something. Find out what it's waiting for (hardware interrupt? open file?) and fix that, and you can let it close cleanly.

这篇关于自动化Killall然后Killall 9级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆