在shell脚本中执行kill命令后未执行任何命令 [英] No command executed after performing kill command in shell script

查看:478
本文介绍了在shell脚本中执行kill命令后未执行任何命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的shell脚本:

Here is my shell script:

#!/bin/bash
PIDS=$(ps -e | grep $1 |grep -v grep| awk '{print $1}')
kill -s SIGINT $PIDS
echo "Done sendings signal"

我将进程的名称作为命令行参数传递.
尽管目标进程实际上正在接收SIGINT信号并退出,但是echo命令没有得到执行.

I am passing the name of the process as command line argument.
The echo command is not getting executed, although the target processes are actually receiving the SIGINT signal and exited.

有什么建议吗?

更新:
我将代码更改为:

Update:
I changed the code to:

#!/bin/bash
PIDS=$(ps -e |grep $1 | grep -v grep | awk '{print $1}'|grep -v $$)
echo $PIDS
kill -s SIGINT $PIDS
echo "Done sendings signal"
echo "The current process is $$"

现在我注意到了一件奇怪的事情:
该脚本正在运行,但未达到预期的效果.在脚本之外的命令行中执行以下命令
ps -e|grep process-name|grep -v grep|awk '{print $1}'|grep -v $$
给出进程名称的pid,但是当我在shell脚本中执行同一命令时,将其分配给变量PIDS,然后回显PIDS,则除了进程名称的pid之外,它还显示一个pid.因此,当执行kill命令时,它会给出一个错误,即具有第二个pid的进程不存在.它确实在终端中回显剩余的句子.有什么线索吗?

Now I am noticing a strange thing:
The script is working but not as expected. Executing following command in command line outside the script
ps -e|grep process-name|grep -v grep|awk '{print $1}'|grep -v $$
gives pid of the process-name but when I execute the same command inside shell script, assign it to variable PIDS and then echo PIDS then it shows one more pid in addition to the pid of process-name. Therefore when the kill command executes it gives an error that the process with second pid doesn't exist. It does echo the remaining sentences in the terminal. Any clue ?

推荐答案

您的脚本有效.我能看到未执行回显的唯一原因是$ 1的某些值和脚本文件名结合在一起,以便您的脚本PID也被收集,从而使脚本自杀.

Your script works. The only reason I can see for the echo not being executed is that some value of $1 and the script file name combine so that your script PID is also gathered, thereby making the script suicide.

PIDS行产生一个运行ps,grep和另一个grep的进程-这样您就不会在PIDS中找到运行grep的进程,但是父进程本身呢?

The PIDS line spawns a process running ps, grep, another grep -- so you won't find in PIDS the processes running grep, but what about the parent process itself?

尝试:

#!/bin/bash
PIDS=$(ps -e | grep $1 |grep -v grep | awk '{print $1}' | grep -v "^$$\$" )
kill -s SIGINT $PIDS
echo "Done sendings signal"

或以适当的安全抓斗一个接一个地铺设管道.

or run the pipes one after the other with suitable safety greps.

很明显,"$ 1"选项选择过多.所以我要像这样重写脚本:

it is evident that the "$1" selection is selecting too much. So I'd rewrite the script like this:

#!/bin/bash
# Gather the output of "ps -e". This will also gather the PIDs of this
# process and of ps process and its subshell.
PSS=$( ps -e )
# Extract PIDs, excluding this one PID and excluding a process called "ps".
# Don't need to expunge 'grep' since no grep was running when getting PSS.
PIDS=$( echo "$PSS" | grep -v "\<ps\>" | grep "$1" | awk '{print $1}' | grep -v "^$$\$" )
if [ -n "$PIDS" ]; then
    kill -s SIGINT $PIDS
else
    echo "No process found matching $1"
fi
echo "Done sending signal."

这篇关于在shell脚本中执行kill命令后未执行任何命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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