[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"