今天在写脚本的时候遇到一个问题,需求是通过本机远程连接到其他服务器,杀掉之前提起的nmon进程,然后再返回本机
于是命令如下(以本机localhost为例):
ssh localhost "ps -ef | grep "nmon" | grep -v grep | awk -F ' ' '{print $2}'|tail -1| xargs kill -9;exit"(ssh已经做了无密码登录,提起nmon进程的脚本没问题,问题只出现在kill进程)
执行以上命令后,这时问题出现了
返回信息:
[root@svt112 nmon]# ssh localhost "ps -ef | grep "nmon" | grep -v grep | awk -F ' ' '{print $2}'|tail -1| xargs kill -9;exit"Usage:
kill [options] <pid|name> [...]Options:
-a, --all do not restrict the name-to-pid conversion to processes
with the same uid as the present process
-s, --signal <sig> send specified signal
-q, --queue <sig> use sigqueue(2) rather than kill(2)
-p, --pid print pids without signaling them
-l, --list [=<signal>] list signal names, or convert one to a name
-L, --table list signal names and numbers -h, --help display this help and exit
-V, --version output version information and exitFor more details see kill(1).单独执行 ps -ef | grep "nmon" | grep -v grep | awk -F ' ' '{print $2}'|tail -1| xargs kill -9 是没有问题的,但是不知道为什么加上ssh去执行就失败了
于是命令如下(以本机localhost为例):
ssh localhost "ps -ef | grep "nmon" | grep -v grep | awk -F ' ' '{print $2}'|tail -1| xargs kill -9;exit"(ssh已经做了无密码登录,提起nmon进程的脚本没问题,问题只出现在kill进程)
执行以上命令后,这时问题出现了
返回信息:
[root@svt112 nmon]# ssh localhost "ps -ef | grep "nmon" | grep -v grep | awk -F ' ' '{print $2}'|tail -1| xargs kill -9;exit"Usage:
kill [options] <pid|name> [...]Options:
-a, --all do not restrict the name-to-pid conversion to processes
with the same uid as the present process
-s, --signal <sig> send specified signal
-q, --queue <sig> use sigqueue(2) rather than kill(2)
-p, --pid print pids without signaling them
-l, --list [=<signal>] list signal names, or convert one to a name
-L, --table list signal names and numbers -h, --help display this help and exit
-V, --version output version information and exitFor more details see kill(1).单独执行 ps -ef | grep "nmon" | grep -v grep | awk -F ' ' '{print $2}'|tail -1| xargs kill -9 是没有问题的,但是不知道为什么加上ssh去执行就失败了
ps -ef | grep "nmon" | grep -v grep | awk -F ' ' '{print $2}'|tail -1
嗯,时能够正常输出pid的,如果单独执行 ps -ef | grep "nmon" | grep -v grep | awk -F ' ' '{print $2}'|tail -1| xargs kill -9,是能够杀死指定nmon进程的,但是加到ssh远程命令里,就不行了,感觉像是ssh $IP " " 里获取不到pid变量
ps -ef | grep nmon | grep -v grep | awk -F ' ' '{print }'|tail -1| xargs kill -9;exitawk命令的$2没有了
a='' b='$var_b'
#判断tomcat是不是假死
PID=$(ps -ef |grep tomcat |grep /u01/web/tomcat1 |grep -v 'grep'|awk '{print $2}' | xargs)
if [ "$PID" ]
then
echo "正在kill进程"
kill -9 $PID
else
echo "没有进程需要kill"
fi