您的位置:首页 > 运维架构 > Shell

Shell编程实现Kill线程

2015-10-13 14:38 639 查看
[root@centos65 liumengyang]# cat test.sh
#!/bin/sh
#if the process exists,kill it
pid=$(ps -ef|grep java|awk '{print $2}')
pidname=$(ps -ef|grep java|grep -v 'grep'|awk '{print $1}')
#judge if the process is running.if not,restart.
if [ $pidname ]
then
echo "the process is running.."
else
echo "the process is not existing.."
fi
[root@centos65 liumengyang]#


(1)grep -v 'grep'命令的作用是把grep进程顾虑掉

(2)把命令输出值赋值给变量pid pid=$(ps -ef|grep java|awk '{print $2}')

(3)awk '{print $1} 命令是把一行文字按照空格分成一个数组的形式,然后通过'$1'取值。

比如"liu meng yang" 通过awk分词以后,‘$1’对应"liu",'$2'对应"meng",'$3'对应"yang"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: