3

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

脚本文件 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 &

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

4

1 回答 1

2

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

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

虽然如果你想要命令的 pid,temp_tool我会建议这样做:

ps -C temp_tool -o pid

而不是ps -ef | grep ...

于 2013-07-29T10:20:42.113 回答