gpt4 book ai didi

linux - 在 shell 中使用 "kill"后如何继续执行下一个其他命令?

转载 作者:太空狗 更新时间:2023-10-29 11:15:00 24 4
gpt4 key购买 nike

我想将两个不同的脚本文件合并到一个脚本文件中,该文件可以完成两个不同文件的功能。脚本文件是:

脚本文件A:

pid=`ps -ef | grep temp_tool | grep -v grep | awk '{print $2}'`
kill -9 ${pid}

脚本文件 B:

nohup ./temp_tool &

合并后的脚本文件:

pid=`ps -ef | grep temp_tool | grep -v grep | awk '{print $2}'`
kill -9 ${pid}
nohup ./temp_tool &

执行kill命令后整个合并的脚本文件会停止,我必须将其修改为:

pid=`ps -ef | grep temp_tool | grep -v grep | awk '{print $2}'`
out=`kill -9 ${pid}`
nohup ./temp_tool &

现在效果很好,但我不知道为什么?有区别吗?

最佳答案

我会说 $pid 还包含您脚本的 pid。您可以将其过滤掉:

script_pid=$$
pid=$(ps -ef | grep temp_tool | grep -Ev "grep|$script_pid" | awk '{print $2}')

不过如果你想要命令 temp_tool 的 pids,我建议这样做:

ps -C temp_tool -o pid

而不是 ps -ef | grep ...

关于linux - 在 shell 中使用 "kill"后如何继续执行下一个其他命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17919077/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com