通常检查一个程序是否运行可以使用ps -ef|grep 程序名称的方法来检查,如果项将其写入shell脚本让程序自动检测并给出状态可以用如下写法:
server_stat=`ps -ef|grep StartServer|grep -v grep|awk '{print $2}'`
if [ ! -z $server_stat ];
then
echo "the server service is running"
else
echo "the server service is not running"
注意:
grep -v grep可以将前面提取出来的带有grep StartServer这个进程给筛掉;
之所以加awk '{print $2}'是因为有时候前面的命令抓取到的数据过长,运行时程序会认为有太多参数而无法运行,故使用此语句截取一段;